This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Variables, expressions, and statements.


http://dpaste.com/1764212/

Lets look at three components of many programming languages.

Three primary tools we will be working with are:

  • Statements - small parts of a program that do not necessarily return a result. A program is formed by a sequence of one or more statements.
  • Names (a.k.a. variables) - pointers to data stored in memory.
  • Expressions - combination of names, symbols,  functions, and/or operators that return another value.

These three aspects are fundamental to programming in general and will be explored throughout this course.

Examples:
In Python, statements can take many forms and work with many programming elements such as names and operators.

Names are labels that point to a specific item in the computer's memory. Assignment statements are statements where we assign a name to a value, or memory location. Below, a_name points to the memory location of a_value.

>>> a_name = 'a_value'

Names, technically called identifiers, must start with an underscore (the "_" character) or a letter. Names can contain any sequence of letters, numbers, and underscores. Names cannot contain spaces or other special characters. Names are case sensetive, so 'Sunshine' and 'SUNSHINE' are two different names.

There are 33 reserved words that may not be used as names. The following table outlines Python's reserved words.

Python Reserved Words
False None True and assert
break class continue def del
else except finally for from
global import in is lambda
nonlocal not pass raise return
try while with    
         

Expressions typically return some result. The result can then be assigned a name.

In the following example, the expression on the right hand side will be evaluated and then assigned to the name to the left of the '=' assignment operator.

>>> total = 35.9 + 24.29
>>> total
60.19

Basic examples of expressions include arithmetic operations. Operators are included in expressions and consist of symbols such as:

Common Operators
+ - / * **

 

Here are some operators in action:

>>> 2 + 3 # Addition
5
>>> 3 - 2 # Subtraction
1
>>> 5 * 5 # Multiplication
25
>>> 10 / 5 # Division
2
>>> 3 ** 3 # Exponent
27

Learning Resources

Assignment

  1. Review the documents in the learning resources section.
  2. Be sure to complete the excercises as we are here to practice.
  3. Copy your completed excercises and share them via a pastebin service such as dPaste.

If you have any questions or difficulties post them here and we will work together to make sure that everyone has a positive learning experience. Please also post ideas and insights into these concepts. We can learn a lot from each others' perspectives and experience :-)

Task Discussion


  • Anonym said:

    I have both Python 2 and 3 (on Ubuntu 10).  If I deinstall Python2 totally via Synaptic Package Manager, would it affect any of the packages for Py3?

    Also, when I run the following

    name = input ('What is your name?\n')
    print ('Hello ',name,'!')
     

    it gives an unwanted space before '!'.  How can I get rid of the space?

    on Nov. 9, 2011, 6:21 p.m.
  • Anonym said:

    I have a question.  I completed the first exercise (Chap 2- 'Variables, expressions, and statements') using Python 2.6 (on LINUX), which I had installed some time ago.  I decided to install Python 3.2, which I did today and tried the first exercise again.   I got the error message:  NameError: name 'raw_input' is not defined. In fact, none of the command lines, such as 'print', does not work in 3.2. Could someone explain to me what is going on?  Thank you.
     

    on Nov. 4, 2011, 6:46 p.m.

    Shannon said:

    raw_input() was renamed to input ().

    print statement has been replaced with a print () function.

    For details, search (cntr+f) for 'input' and 'print' on 

    http://docs.python.org/release/3.0.1/whatsnew/3.0.html

    on Nov. 4, 2011, 7:49 p.m. in reply to Anonym

    Anonym said:

    Thank you for the link.

    on Nov. 8, 2011, 10:16 p.m. in reply to Shannon
  • Anonym said:

    Did the first ex.

    http://pastebin.com/P512BJ2L

    on Nov. 4, 2011, 10:18 a.m.
  • Evita said:

    Exercises 2.2-2.3-2.4-2.5 separated by comments :

    http://pastebin.com/WzzTcUaT

    on Oct. 6, 2011, 4:13 a.m.
  • Gniyophyte said:

    Is anyone having difficulty copying their assignments and posting/pasting to somewhere?  I've got Python installed, working, and not able to copy/cut/paste etc.  I can take a screen shot, but I imagine by the time I get to writing the Nobel Prize winning code for something, it won't fit on my screen.

    on Oct. 5, 2011, 9:05 p.m.

    Shannon said:

    Do you know if you are using IDLE as an editor? IDLE opens in a window with "Python Shell" in the top blue bar and on the taskbar button, has familiar Windows options at top (File, Edit , etc)?

    If so, you should be able to select text and use keyboard shortcuts to copy (Ctrl + C), but you can't right-click for such editing options.

    If you're not using IDLE, as a new user, I recommend trying to find it. Hopefully you can find it in your start menu under All Programs > Python > IDLE.

    on Oct. 7, 2011, 8:18 p.m. in reply to Gniyophyte
  • Mars83 said:

    on Oct. 3, 2011, 3:14 p.m.
  • Gniyophyte said:

    Oh, deflation!

    I had done exercise 1, assignment whatever aka 2.2, which is slide 25 by the way according to everyone else's assignment. 

    I did it.  It worked.  But, there is no "copy" feature from which I could paste it here.  Also, there are no line numbers showing when I write code.  Any hints?

    on Oct. 2, 2011, 7:26 p.m.

    Captcheese said:

    To copy the code to say a service like pastebin, you can just select all the code and press the button combination Ctrl + V in pastebin.  In regards to the line numbers, what editor or IDE are you using? If it is IDLE, I don't think the feature actually exists in IDLE. You could choose to use another editor such as Notepad ++ but be wary of mixing tabs and spaces when indenting. 

    on Oct. 2, 2011, 8:35 p.m. in reply to Gniyophyte
  • Gniyophyte said:

    I have reviewed the assignments posted by the three or four previous commenters.  I wonder why they are taking Python 101?  Obviously they have some background knowledge and are applying it quite well.  Is there a Python Beginner A level 1 Pre-Computer Age course?

    on Oct. 2, 2011, 7:08 p.m.
  • Gniyophyte said:

    Okay, so back in CaveMan Days our teacher, Og, told us this: Do assignment 1.  Furthermore, he told us what the assignment was:  "Make a spatula out of a rock."  However, here at Task 2 I have no idea what the assignments are.  I'm told to do the exercises, but I find no exercises.  I have copied the program samples shown in the slide show/streaming video.  I can make more sense of Python than of the English explanations.  Can someone tell me what the exercises cum assignments are?

    on Oct. 2, 2011, 6:54 p.m.

    NamKeph said:

    Chapter 2 of Python for Informatics has some exercises.

    on Oct. 7, 2011, 2:26 a.m. in reply to Gniyophyte
  • dean said:

    My answers:

    http://goo.gl/a1Jrp

    on Oct. 2, 2011, 11:42 a.m.
  • Jeff said:

    My complete exercises

    http://pastebin.com/Xei7ucLp

    on Sept. 28, 2011, 4:39 p.m.
  • NamKeph said:

    Here are my exercises:

    http://pastebin.com/1bjywgGg

    on Sept. 22, 2011, 8:46 a.m.
  • Sage Ross said:

    I didn't realize that there was so much incompatibility between Python 2.x and Python 3.x.  A few weeks ago, I started learning Python through Project Euler, and I've done about 20 problems so far.  On my computers, the default "python" is Python 2, and you have to run scripts with "python3" to use Python 3.  So I'd been learning Python 2, but decided to try 3 for this.  There were a lot more hangups than I expected!  Just something to be aware of.

    For these exercises, the relevant things include:

    • 'print ("something")' instead of just 'print "something"'
    • input() instead of raw_input()
    on Sept. 16, 2011, 9:49 a.m.

    Brylie Oxley said:

    There is a utility, called 2to3, that will convert programs from Python 2 to Python 3.

    http://docs.python.org/library/2to3.html

    on Oct. 5, 2011, 8:10 p.m. in reply to Sage Ross
  • emilfos said:

    Here is my code.

    http://pastie.org/2540497

    on Sept. 15, 2011, 8:18 p.m.
  • Luiz Fukushiro said:

    here's mine:

    http://pastebin.com/TAgEXejU

    on July 16, 2011, 12:35 a.m.
  • EmperiuM said:

    My exercices http://pastebin.com/6s9wNa6M

    I think my code could be improved.

    Please, give me some feedback.

    Thank you 

    EmperiuM

    (exercises 2.2, 2.3, 2.4 and 2.5)

    on July 12, 2011, 6:53 a.m.

    ajinkya said:

    Hi. Your code is good and well commented. Although you can make 1 minor improvement.

    Using a space on both sides of the equal two sign while assigning is a Good practice and increases readibility.

    a=1      #NO
    
    a = 1    #YES
    

    on July 12, 2011, 10:25 a.m. in reply to EmperiuM

    EmperiuM said:

    OK, thank you for telling me that. I'll keep that in mind for the future and I'll change on this code.

    on July 12, 2011, 10:59 a.m. in reply to ajinkya

    Brad Olson said:

    If you want to know other best practices you could read PEP-8: http://www.python.org/dev/peps/pep-0008/

    on July 12, 2011, 11:22 a.m. in reply to EmperiuM

    EmperiuM said:

    Thank you mate :) I'll read it :)

    on July 12, 2011, 12:01 p.m. in reply to Brad Olson
  • Brad Olson said:

    on July 8, 2011, 10:15 a.m.
  • Zolomon said:

    https://gist.github.com/1062162 - my exercise solutions!

    on July 3, 2011, 7:22 a.m.
  • Sudaraka Wijesinghe said:

    My py4int exercise 2 code

    Exercise 2.2

    name = raw_input('Enter your name: ')
    print 'Hello', name
    

     

    Exercise 2.3

    # Get hours from the user
    hours = raw_input('Enter number of hours: ')
    f_hours = float(hours)
    
    # Get rate from the user
    rate = raw_input('Enter rate per hour: ')
    f_rate = float(rate)
    
    # Calculate pay
    pay = f_hours * f_rate
    
    # Output calculated pay
    print 'Gross pay:',pay
    

     

    Exercise 2.4

    width = 17
    height = 12.0
    
    print width/2 # Result 8 (int)
    print width/2.0 # Result 8.5 (float)
    print height/3 # Result 4.0 (float)
    print 1 + 2 * 5 # Result 1+(2*5) -> 1+10 -> 11 
    
    

     

    Exercise 2.5

    inp = raw_input('Enter temperature in Celsius: ')
    temp_c = float(inp)
    
    temp_f = (9.0 / 5.0) * temp_c + 32
    
    print 'Temperature in Fahrenheit is:', temp_f
    
    

    on June 25, 2011, 1:24 p.m.