Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

for loop

Hi

Can anybody tell me how to convert the following c++ code to python:

for (int x =0; x < 100; ++x){
     y[x] = sin( i * 20 );
}
0
IssacJones
Asked:
IssacJones
  • 4
  • 2
2 Solutions
 
peprCommented:
Try the following:

a.py
import math

y = []    # empty list used as empty dynamic aray
print y   # to visualize what we have before            
for x in xrange(100):
    y.append(math.sin(x * 20))
    
print y   # what is inside after
print '-' * 70

# The alternative approach to simulate better the array.  The list
# of 100 elements is created first.  Then the "normal" indexing
# can be used to access and modify the elements.
y = [0] * 100
print y   # to visualize what we have before            
for x in xrange(100):
    y[x] = math.sin(x * 20)
print y   # what is inside after

Open in new window

0
 
IssacJonesAuthor Commented:
Hi pepr

As you can gather I'm learning Python so please forgive the novice questions.

I note you use xrange(100). What if I was using

for (int x=5; x<100; ++x)

I'm slightly surprised that the Python code is so different to C++. I was expecting more of an overlap e.g. Java, I believe, is more like C++.

As a matter of interest, why was Python written so differently?

Issac
0
 
zvytasCommented:
Hi, should be as follows:

for x in range(0,100):
   y[x] = math.sin( i * 20 )
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
peprCommented:
The Python for-loop construct is not the counted loop as in C/C++.  It is rather for_each kind of loop.  The xrange(100) returns the iterator that goes from 0 to 99.  You can also use the range(100) that returns the list of 0 to 99.  When the list is used in the for loop, it is to be iterated.  The effect is the same.  The xrange() will be more memory efficient for big sequences.

However, there is numpy package that is more optimized for working with arrays and matrices.
0
 
peprCommented:
I tried the matplotlib just now, but it requires also numpy, and I haven't found the numpy for my environment (Python 2.7 64-bit on Windows).  But you possibly can try the following:

b.py
import math
import matplotlib.pyplot as plt

y = [0] * 100
for x in xrange(100):
    y[x] = math.sin(x * 20)
 
plt.plot(y)
plt.ylabel('sin(x)')
plt.show()

Open in new window


I have no idea if it works and how.  It is taken directly from the tutorial http://matplotlib.sourceforge.net/users/pyplot_tutorial.html (combined with the above sin).
0
 
peprCommented:
More about the for loop.  The C/C++ loop is not actually a counted loop (in comparison with say Pascal for-loop).  It is rather generalized while-loop with initialization commands and the "next" written syntactically before the body.

The Python for-loop is more abstract.  It was designed to simplify looping through anything that supports iteration.  This way you can get elements of lists, elements of tuples, elements of strings, elements of sets, keys of dictionaries, lines of text files, and values of whatever containers that supports iteration.
0
 
IssacJonesAuthor Commented:
Thanks guys
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now