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


  • skar said:

    Here are my answers: http://pastebin.com/vQbKeph7 . Please see if I have done them right. I am new here.

    on May 18, 2011, 10:20 p.m.
  • Josue said:

    Finally was able to figure Github (actually took me longer than the exercises). HERE is the code for the chapter 2 exercises

    on May 18, 2011, 4:21 a.m.
  • Fernando Aguilar said:

    Why do I feel like I'm the only true 'noob' here? smiley  I'm actually catching on though so I'm stoked!

    Ex2.2

    name = raw_input('What is your name?\n')
    print ('Hello ') + name

    Ex2.3

    hours = raw_input('Enter Hours:\n')
    rate = raw_input('Enter Rate:\n')
    print('Payscale ='), float(hours)*float(rate)

    Ex2.4

    width = 17
    height = 12.0

    width/2=8 (integer)

    height/3=4.0 (float)
    1 +2 * 5=11 (integer)
     

    Ex2.5

    tempC = raw_input('Enter the temperature in Celsius:\n')
    tempF = (1.8) * float(tempC) + 32
    print 'Your converted temperature in Farenheit is:', tempF

    on May 18, 2011, 1:25 a.m.
  • Todd Hayes said:

    Here is my code for Exercise 2.5, on Pastie:

    http://pastie.org/1896979

    on May 13, 2011, 10:34 a.m.
  • Todd Hayes said:

    Here is my code for Exercise 2.2, on Pastie:

    http://pastie.org/1896942

    on May 13, 2011, 10:26 a.m.
  • Todd Hayes said:

    Here's the link to exercise 2.3, on Pastie:

    http://pastie.org/1896845

    on May 13, 2011, 9:43 a.m.
  • Josep Sanchez said:

    Here is my code. Thanks for your feedback.
    on May 12, 2011, 6:16 p.m.
  • Anonym said:

    I have all of the excersises done up to chapter 3 https://github.com/Quantum-Guru/Python-Programming-101

    on May 12, 2011, 4:24 p.m.
  • Vladimir Támara Patiño said:

    2.2

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

    2.3

    hours = raw_input('Enter Hours: ')
    rate = raw_input('Enter Rate: ')
    print 'Pay: ', float(hours)*float(rate)
    

    2.4

    width/2 is integer 8
    width/2.0 is float 8.5
    height/3 is float 4.0
    1 + 2 * 5 is integer 11
    

    2.5

    celsius = raw_input('Enter temperature in Celsius: ')
    print 'In Farenheit it is: ', 32.0 + float(celsius)*9.0/5.0
    
    

    The only error I found in section 2 is that the exercises begin with 2.2 instead of 2.1

    In section 1 in page 15, in exercise 1.10 the name of the variable should be in lowercase (x instead of X).

    on May 5, 2011, 4:38 p.m.
  • Pablo Olmos de Aguilera C. said:

    Where I'm supposed to show you the exercises result? Something like Tylier did?

    on April 30, 2011, 5:03 p.m.

    Brylie Oxley said:

    You can add your excercise results to this discussion thread or post the on any code sharing site such as Github or Pastie.

    on May 3, 2011, 12:23 a.m. in reply to Pablo Olmos de Aguilera C.

    Pablo Olmos de Aguilera C. said:

    Thanks! I think I'll use Github =)

    on May 4, 2011, 9:46 p.m. in reply to Brylie Oxley
  • Anonym said:

     

    2.1
    5
    x+5
    x+1
    2.2
    name = input('Enter your name: ')
    print ('Hello '+name)
    2.3
    h=input("Enter Hours: ")
    r=input("Enter Rate: ")
    p=int(h)*float(r)
    print('Pay:',p)
    2.4
    width=17
    height=12.0
    print(width/2)
    print(width/2.0)
    print(height/3)
    print(1+2*5)
    2.5
    c=input('Convert Celsius to Fahrenheit\nEnter Temperature:')
    conversion=float(c)*9.0/5.0+32
    print(conversion)
    on April 28, 2011, 12:12 p.m.
  • etwillis said:

    Hello,

    Anyone have a suggestion for a good python ide-editor for Windows?

    Thanks

    on April 27, 2011, 7:17 p.m.

    Anonym said:

    NINJA IDE i have heard is good

    on April 27, 2011, 7:22 p.m. in reply to etwillis

    etwillis said:

    Thanks Wesley. Gonna check it now now. I´m new to this so feeling a bit lost...thanks for the recommendation.  Just came across this as well

     

    http://stackoverflow.com/questions/60784/poll-which-python-ide-editor-is-the-best

    on April 27, 2011, 7:24 p.m. in reply to Anonym

    Brylie Oxley said:

    Python comes with a good editor called IDLE. More info on IDLE can be found here:

    http://docs.python.org/library/idle.html

    on April 28, 2011, 6:57 p.m. in reply to etwillis

    Anonym said:

    There are better though much better. IDLE is hard to work with for a first time programmer because of the way you compile with it. It creates a whole other screen for the program when it runs.

    on April 30, 2011, 3:45 p.m. in reply to Brylie Oxley
  • Tyler Cipriani said:

    Chapter 2 exercises posted on my github page (here) - Full code with try/except blocks posted as a single Python script for review or download.

    on April 27, 2011, 2:52 p.m.