hello there, i started learning python yesterday and im having quite a bit of difficulty following tutorials...

I was following this tutorial

but most of what i wrote did not work. How do i make the code compatible with python 3.0?.

for instance it says

lucky=7
print lucky

but when i do this, it says "invalid syntax"

but if i simply type lucky and press enter it works???:confused:

and here's another...

changing = 3
print changing
 
changing = 9
print changing 
 
different = 12
print different
print changing
 
changing = 15
print changing

also what is the difference between the single quotes double
quotes when declaring strings?:-/
-----------------------------------

outputs...

>>> if 1: print ("true")
print ("done")
SyntaxError: invalid syntax (<pyshell#1>, line 2)
>>> print ("hello world!")
hello world!
>>> print ("hello world!"),
print ("who the heck are you?")
hello world!
(None,)
>>> print ("hello world!")
print ("who the heck are you?")
hello world!
>>> lucky=7
>>> print lucky
SyntaxError: invalid syntax (<pyshell#6>, line 1)
>>> print ("lucky")
lucky
>>> lucky
7
>>> print one=2
SyntaxError: invalid syntax (<pyshell#9>, line 1)
>>> one=2
>>> one
2
>>> two=2
>>> one two
SyntaxError: invalid syntax (<pyshell#13>, line 1)
>>> one, two
(2, 2)
>>> red=5
>>> blue=10
>>> print red,blue
SyntaxError: invalid syntax (<pyshell#17>, line 1)
>>> red,blue
(5, 10)
>>> yellow=red
>>> red,blue,yellow
(5, 10, 5)
>>> print ("hello world")
hello world
>>> print (\"hellow world"\)
	
SyntaxError: unexpected character after line continuation character (<pyshell#22>, line 1)
>>> print (\"hello world\")
SyntaxError: unexpected character after line continuation character (<pyshell#23>, line 1)
>>> print ("bouncy")*2
bouncy
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    print ("bouncy")*2
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
>>> print ("bouncy")
bouncy
>>> one=2
>>> one*2
4
>>> print ("hello")+2
hello
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    print ("hello")+2
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
>>> print ("helo")*2
helo
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    print ("helo")*2
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
>>> print len ("hello world!")
SyntaxError: invalid syntax (<pyshell#30>, line 1)

sorry for all the newbness and long post:$ just needa get differences between the version sorted so i can continue with the tutorials

Sweet, I finally get to reply to this post and show off how smart I am!

The reason your code is not working is that you're using a tutorial designed for an older version of python. Python 3.0 only recently came out and they did not maintain backward compatibility. The only change that you need to worry about at this point in your noobiness is that print works differently. Now you have to enclose stuff in parenthesis.

lucky = 7
print(lucky)

cheers mate. Do you know of any indepth python 3.0 tutorials or any good tutorials for that matter?.

Here is a link to the beginning python thread. http://www.daniweb.com/forums/thread20774.html Jump ahead to the last page and you'll find a really good explanation of the print function in python 3.0.

Otherwise, I think that python 3.0 is new enough that there aren't many tutorials that are aimed directly at python 3K. Mostly you'll find tutorials showing the differences between python 2.6 and python 3.0.

I think you can probably keep using the tutorials that you're using now, just remember to wrap your print statements in the parenthesis.

why don't you start with 2.6? I suggests this for newbies as there are pretty many tutorials and books there. Only when you have grasped the basics you can consider Py3k. Also few (I guess none) of non standard modules aren't yet out for python!

If you change your mind and go for 2.x then books are there:
Think Python - Bruce Eckel
A byte of python - Swaroop C.H (also py3k is out )
Dive into Python - Mark Pilgrim
All these are available on net plus many many tutorials

why don't you start with 2.6? I suggests this for newbies as there are pretty many tutorials and books there. Only when you have grasped the basics you can consider Py3k. Also few (I guess none) of non standard modules aren't yet out for python!

If you change your mind and go for 2.x then books are there:
Think Python - Bruce Eckel
A byte of python - Swaroop C.H (also py3k is out )
Dive into Python - Mark Pilgrim
All these are available on net plus many many tutorials

Don't bother with Python26! It does not prepare you for Python30.

hello there could some please answer this...

def func(a, b=5, c=10):
    print('a is', a, 'and b is', b, 'and c is', c)
 
func(3, 7)
func(25, c=24)
func(c=50, a=100)

on here why not just say func(25,24P) instead of func(25,c=24)
etc?

edit:
also could someone please explain me these lines of code

def total(initial=5, *numbers, **keywords):
    count = initial
    for number in numbers:
        count += number
    for key in keywords:
        count += keywords[key]
    return count
 
print(total(10, 1, 2, 3, vegetables=50, fruits=100))

and the use of the '*'?

What the asterix does is that is distinguishes the non-keyword arguments and the keyword arguments. So for your function there you have one value, initial, that is set and will always be in your function, you need it to be there for the function to run. Now, when you go something like total(10,1) there is no place that you have specified that is used to store that extra 1 so what happens to it? It is put into an array of non-keyword arguments that in your function is called numbers. So for this one: total(10,1,2,3) The numbers 1,2 and 3 are ALL in the numbers array.

The next bit is the **keywords. This bit means that you must supply keywords as well.
Soo total(1,another = 4,anotheragain = 5) those two arguments (another and anotheragain) have not been specified as arguements in your function, YET they have keywords, so they are stored in a Dictionary, here it is called keywords, so for the function call:

total(1,2,3,4,5,hi = 2, world = 3, test = 5) 
#there would be the following values:

#initial = 1 
#numbers = [2,3,4,5]
#keywords = {hi:2,world:3,test:5}

I hope you understand better now.. if not, just ask for any clarification on things you dont.

Oh and for the first question, the reason that you have to do that, is that when supplying arguments to functions they are given in order, so if you want to skip giving B a value then you have to tell the program that you want to go straight to giving C a value.

Don't bother with Python26! It does not prepare you for Python30.

I didn't meant to introduce him/her to python 3 but rather to python language in general

If I would be a beginner, I would simply start with Python30 and Swaroop's online tutorial book. If you bump into old code and have compatibility problems ask, there are enough old timers here to help. Or you could always try the 2to3.py utility that comes with Python30.

Enjoy our learning experience, Python makes it easy! Well, at least when compared to other computer languages.

What the asterix does is that is distinguishes the non-keyword arguments and the keyword arguments. So for your function there you have one value, initial, that is set and will always be in your function, you need it to be there for the function to run. Now, when you go something like total(10,1) there is no place that you have specified that is used to store that extra 1 so what happens to it? It is put into an array of non-keyword arguments that in your function is called numbers. So for this one: total(10,1,2,3) The numbers 1,2 and 3 are ALL in the numbers array.

The next bit is the **keywords. This bit means that you must supply keywords as well.
Soo total(1,another = 4,anotheragain = 5) those two arguments (another and anotheragain) have not been specified as arguements in your function, YET they have keywords, so they are stored in a Dictionary, here it is called keywords, so for the function call:

total(1,2,3,4,5,hi = 2, world = 3, test = 5) 
#there would be the following values:

#initial = 1 
#numbers = [2,3,4,5]
#keywords = {hi:2,world:3,test:5}

I hope you understand better now.. if not, just ask for any clarification on things you dont.

Oh and for the first question, the reason that you have to do that, is that when supplying arguments to functions they are given in order, so if you want to skip giving B a value then you have to tell the program that you want to go straight to giving C a value.

this may be a dumb questions sorry for that:$ , but what is the difference between keyword-only parameters and non-keyword?.

If I would be a beginner, I would simply start with Python30 and Swaroop's online tutorial book. If you bump into old code and have compatibility problems ask, there are enough old timers here to help. Or you could always try the 2to3.py utility that comes with Python30.

Enjoy our learning experience, Python makes it easy! Well, at least when compared to other computer languages.

thanks, sneekula, i did go along with the swaroopes online tutorial.
cheers:) .
-------------------------------------------

Sorry for bombarding the python forums guys but ah could you explain these lines of code to me as well?.

import sys
 
print('The command line arguments are:')
for i in sys.argv:
    print(i)
 
print('\n\nThe PYTHONPATH is', sys.path, '\n')

questions i have here are
what is the sys.path variable? and whats the arg variable on here?

was following this

Here is the documentation for sys.path http://docs.python.org/3.0/library/sys.html#sys.path, and here is the documentation for sys.argv http://docs.python.org/3.0/library/sys.html#sys.argv.

At this point, you should start using python's documentation: add this page http://docs.python.org/3.0/ to your favorite pages :) A nice link in this page is the global module index. In this index you can find the sys module and other modules which come with your python distribution. Also note the link in the upper left corner which allows you to download the whole documentation to your computer for faster access.

The difference between a non-keyword and a keyword is the way you supply the arguments.
a = 2
is a keyword argument, a is the keyword, 2 is the value
if you just supplied 2 then it is a non-keyword argument.

thanks guys, am on modules at the moment...Figuring things out slowly, thanks for the help. Much appreciated:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.