LPTHW Exercises 0-10
(1/13/2014 -- 6:30pm EST)
Add your questions in the Q&A section
(see question mark icon above)
ExerCise 0: The Setup
Why do exercises start at number 0?
What does "python" do when written in the terminal?
"CRTL + C" to stop your Python programs
explain cd, ls, mkdir (Bash basics)
Exercise 1
Python files (aka modules) should end with ".py" extension
Typing "python" into the command line starts the interpreter Typing "python my_file.py" executes that file
Create a file with the code below and run it
-
print 'hello world'
print boy
#### Try to understand what the error messages here mean
Exercise 2: Comments & printing
- # character begins a comment
- #### multiple pounds are acceptable for a comment
Why does the # in print "Hi # there." not get ignored? The # in that code is inside a string, so it will be put into the string until the ending " character is hit. These pound characters are just considered characters and aren't considered comments. How do I comment out multiple lines? Put a # in front of each one.
Exercise 3: Numbers and math
Order of operations as you would expect with math
5 + 10 * 2 --> 25
5 > 10 - 9 -->
#### You should try, though, to always be explicit (generally true in coding)
5 + (10 * 2) --> 25
What is going on here with division?
4 / 3 --> 1
4.0 / 3 --> 1.3333333333333333
What is modulus?
4 % 3 --> 1
Exercise 4: Variables
student_count = 20
auditor_count = 5
total_count = student_count + auditor_count
The words above ending in "_count" are variables
A variable is a name that references (or labels) a value
The program uses variables to keep track of values
The number 20 is a value (we can add it, subtract it, etc)
Here is stored in the variable called "student_count"
print new_variable # What happens if we run this?
print "new_variable" # how about this?
Extra Content: Statements & Expressions
An expression is a piece of code that reduces to a value
5 + 7 # this expression reduces to (or represents) the number 12
2 # reduces to the value 2
'Learn' + 'Code' # reduces to the value 'LearnCode'
A statement does not reduce to a value
a = 5 # assignment of a value to a variable is a statement
print (5 + 7) #printing is a statement (addition here is also an expression)
Exercise 5: Intro to Strings
A string is a sequence of characters surrounded by quotes
"boy" --> string
'boy' --> string
boy --> variable name (you need to assign a value to it somewhere)
"2" --> string
2 --> number
Strings can be "interpolated" (aka variables inserted)
print "Boy named %s" % "John" --> "Boy named John"
print "Boy is %d years old" % 2 --> "Boy is 2 years old"
Exercises 7-10: Printing
To extend strings over multiple lines, use triple quotes
example = """
This is a sample string
spread over multiple lines
"""
To include quotes inside strings
"Alternating between single and double quotes will 'work' just fine"
'It "works" in both directions'
"Alternatively, you can use \"backslashes\" instead"
The newline (\n) is a special character that adds a new line
The Basics: syntax, variables, and IO
By benjaminplesser
The Basics: syntax, variables, and IO
- 1,106