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


  • multivoxmuse said:

    I wrote a script to address all of the parts of the chapter 2 exercises. I tried to show different ways of doing each thing, so there is no consistency in the script, but there are a lot of different approaches toward the same end instead.

    http://dpaste.com/1689144/

     

    on March 5, 2014, 1:31 a.m.

    cstsupport said:

    Greetings from Ireland. Thank you for contacting me. I am out of the office until further notice. I will have intermittent access to my emails. Best Regards, Rosetta Talley
    on March 5, 2014, 1:32 a.m. in reply to multivoxmuse
  • Fer said:

    There goes my paste for all of the 5 exercises: http://dpaste.com/hold/1424681/
    on Oct. 21, 2013, 11:11 a.m.
  • v4lent1na said:

    Here's my paste for ex 2.2 and 2.3 http://pastebin.com/NxWcy0Dc

    on Sept. 9, 2013, 8:19 a.m.

    dvdgc said:

    Hello Sofiastella,

    I've first had assumed your 2.2 was not correct as the print didn't have any space betweens "Hello" and name.  But, I've learnt a new thing in python!! Thanks!  It seems that when printing multiple variables they are not "added" one after other, but they are space-separated.

    So, if we have:

    saludation = "Hello"

    name = "Sofiastella"

    print saludation, name

    is not the same that

    print saludation + name

    which was what I had thought your statement was doing at the beginning.

    on Sept. 9, 2013, 9:09 a.m. in reply to v4lent1na

    v4lent1na said:

    I'm glad my little exercise helped you in some way. I'm still just a newbie myself.

    I kinda learn by doing so I did the exercise over until it worked the way it was supposed to.

    Though I must admit I understand HTML better... blush

    on Sept. 11, 2013, 5:30 a.m. in reply to dvdgc
  • v4lent1na said:

    I've been trying to make exercise 2.2 work for a while this morning, but I keep getting a syntax error. Something's amiss but I don't understand what's wrong. I used the same syntax as in the video.

    It looks like this:

    name = raw_input("Enter your name: ")

    print = "Hello", name

    'name' being the name of the variable (he uses something like lmnop but I don't think that's what makes the difference).

    on Sept. 8, 2013, 7:52 a.m.

    Coffe Bean said:

    Hi, PRINT is a command or function so you should not use an equal sign, as it. Will confuse the interpretation. You could use something like For python 3 print("name:" % name) For python 2 print "name:" % name Happy programming! S. Sent from my iPad
    on Sept. 8, 2013, 9:47 a.m. in reply to v4lent1na

    algotruneman said:

    As Coffe Bean said, you are not going to see an output because you are doing an "invisible" assignment of the "Hello", name to a variable called print. The print command is designed to make variable contents visible, as in

    name = "Bob"

    print "Hello", name

    on Sept. 8, 2013, 10:23 a.m. in reply to v4lent1na

    dvdgc said:

    Actually, Coffe Bean is forgeting just one thing to make this work:

    For Python 3

    print("name:%s" % name)  # %s is saying that name has to be formated as a string

    *Note, if I'm not wrong, in python3 this is suggested to use the format function instead as:

    print("name:{0}".format(name)) or

    print("name: {name}".format(name=name))

    For Python 2

    print "name: %s" % name # same than before, the %s tells that name needs to be formated as a string.

    on Sept. 8, 2013, 4:17 p.m. in reply to Coffe Bean

    Coffe Bean said:

    Too true, I was too hasty in posting my answer! Thanks you. I shall be more careful in the future. Coffee Bean >
    on Sept. 8, 2013, 4:43 p.m. in reply to dvdgc
  • Anna Sakoyan said:

    Here's my code

    http://pastebin.com/ZjKxi8Vi

    Had hard time finding the working link to the textbook with the exercises. Thanks to e.mccormick who shared it earlier: http://www.pythonlearn.com/

    on June 23, 2013, 8:54 p.m.

    algotruneman said:

    Thanks to both of you for the connection to a data version of the excellent Think Python book. Switching away from the math base makes some kinds of understanding easier, I think.

    on June 24, 2013, 8:57 a.m. in reply to Anna Sakoyan

    Anna Sakoyan said:

    Well, in my case, as my math base is genuinely basic, I, on the contrary, feel an urge to learn more about it.

    on June 24, 2013, 4:55 p.m. in reply to algotruneman
  • NerdRage said:

    http://pastebin.com/PVm4axDL

     

    on June 11, 2013, 11:51 p.m.
  • Iftekhar Mohammad said:

    on May 16, 2013, 4:44 a.m.
  • Nel Doozy said:

    Link to learning resources not working for me. No error just a blank page!

    on April 15, 2013, 1:12 a.m.

    e.mccormick said:

    Here is the original source:

    http://www.pythonlearn.com/

    on April 23, 2013, 6:59 p.m. in reply to Nel Doozy

    cstsupport said:

    Greetings from Ireland. Thank you for contacting me. I am currently out of the country with very limited access to my emails. I shall return to the office on Tuesday May 7th 2013. Please be aware that Monday May 6th is a bank holiday in Ireland. During my absence Ms. Aoife Hayes will be delighted to answer your queries. You may reach her at aoife.hayes@cst-international.com Warmest Regards, Rosetta Talley
    on April 23, 2013, 7:02 p.m. in reply to e.mccormick
  • Connie Leung said:

    My solution to the exercises

    http://dpaste.com/hold/1049724/

    on April 7, 2013, 1:04 a.m.
  • Dennis Daniels said:

    Here's the code.

    http://dpaste.com/1025648/

     

    The file is in ipython3 which, IMHO, is a great platform for learning python3.

    on March 17, 2013, 9:44 a.m.
  • Wouter Tebbens said:

    great, here are my exercises: http://dpaste.com/hold/1013198/

    on March 5, 2013, 12:32 p.m.
  • Guayo said:

    hi, here is my exercise

    http://dpaste.com/1002223/

     

    on Feb. 26, 2013, 10:18 p.m.
  • Inkbug said:

    on Feb. 19, 2013, 5:43 a.m.
  • rinckemi said:

    Here are my answers: http://dpaste.com/hold/910826/

    (the link button is not working, sorry)

    on Feb. 5, 2013, 9:28 p.m.
  • Adi Sayoga said:

    on Jan. 25, 2013, 3:56 a.m.