for loops ?

This is the place for queries that don't fit in any of the other categories.

for loops ?

Postby danfan » Tue Oct 29, 2013 11:51 pm

I am trying to figure out how loops work... python 2.7

Code: Select all
xxx = ['dd','ddss'.'dsaa']
for z in xxx:
    print ('xxx', )



what is the point of z? FOR Z IN XXX? is there anything that can be done with z? why is for loops written like this?

EDIT, now i am getting an error
danfan
 
Posts: 14
Joined: Fri Oct 25, 2013 1:33 am

Re: for loops ?

Postby danfan » Tue Oct 29, 2013 11:54 pm

Code: Select all
    execfile(filename, namespace)
  File "/home/dux/untitled1.py", line 9
    xxx =['dd','ddss'.'dsaa']
                           ^
SyntaxError: invalid syntax
danfan
 
Posts: 14
Joined: Fri Oct 25, 2013 1:33 am

Re: for loops ?

Postby danfan » Tue Oct 29, 2013 11:57 pm

now it working, its complicated, there is no simpler way of using loops?

Code: Select all
xxx =['dd','ddss','dsaa']
for z in xxx:
    print (xxx, )
danfan
 
Posts: 14
Joined: Fri Oct 25, 2013 1:33 am

Re: for loops ?

Postby stranac » Wed Oct 30, 2013 12:02 am

z is a name every element of the list gets assigned as you iterate over the list.
For your list, in the first iteration z == 'dd', in the second z == 'ddss', and in the third z == 'dsaa'.

That said, what your code is doing makes very little sense. What is it you really want to do?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: for loops ?

Postby metulburr » Wed Oct 30, 2013 12:05 am

EDIT, now i am getting an error

you separated the last element in the list with a period instead of a comma giving your error:
Code: Select all
xxx = ['dd','ddss'.'dsaa']
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1312
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: for loops ?

Postby danfan » Wed Oct 30, 2013 1:44 am

Code: Select all
xxx = 'I','hate'
for z in xxx:
    print xxx,"kittens"


why is it printing it twice and with ( )

Code: Select all
>>> runfile(r'/home/dux/untitled1.py', wdir=r'/home/dux')
('I', 'hate') kittens
('I', 'hate') kittens
danfan
 
Posts: 14
Joined: Fri Oct 25, 2013 1:33 am

Re: for loops ?

Postby stranac » Wed Oct 30, 2013 1:56 am

Because that's what you're telling it to do.
I suspect you wanted something like:
Code: Select all
words = 'I', 'hate'
for word in words:
    print word,
print "kittens"

or:
Code: Select all
print ' '.join(words), 'kittens'
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: for loops ?

Postby danfan » Wed Oct 30, 2013 2:40 am

Code: Select all
a = 'i love'
for z in a:
  print z,
print " kittens"


str() won't work on letters? only numbers? so i can't do str(i love)
danfan
 
Posts: 14
Joined: Fri Oct 25, 2013 1:33 am

Re: for loops ?

Postby danfan » Wed Oct 30, 2013 2:42 am

is there a video that will explain this in detail? of what you can and can''t do?
danfan
 
Posts: 14
Joined: Fri Oct 25, 2013 1:33 am

Re: for loops ?

Postby micseydel » Wed Oct 30, 2013 2:50 am

danfan wrote:str() won't work on letters? only numbers? so i can't do str(i love)

In Python, the thing between the parens is executed first, and its result passed to the str function.
Code: Select all
i love

is not valid Python.
Code: Select all
"i love"

is perfectly valid, and you can pass that to str, but you'll just get another string back that looks exactly the same (so don't do it).
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1119
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests