
|
Lesson 3 Notes: Variables, Expressions, Statements
- Overview
- Values
- Numbers in Python
- Variables
- Operations
- Composition
- Comments
- Input
- Values
- Strings
- Sets of text, usually
- Denoted by “quotation marks”
- Integers
- Whole numbers, no decimal places
- Floating-point
- Numbers with decimal point values
- Long Integers
- Numbers larger than about 2 billion
- Numbers
- Integers
- Cannot be larger than fixed size (about 2 billion on 32-bit
system using +/-)
- When divided, give only whole number results
- Long Integers
- Signified by L at end of number (23434235234L)
- Working with Numbers
- Use the type you want the results in.
- If you want decimal values, use at least one decimal number in
computation.
- Example: 15 / 2.0 will yield 7.5, 15/2 will yield 2
- If you want large values, use at least one Long in number
computation
- Example: 23423908423908430L / 2
- Strings
- Strings must be inside single or double quotation marks
- name = “This is your name”
- name2 = ‘This is also your name.’
- If the string needs quotation marks, precede the extra “ with a \.
- title = “He said \”I Love You\” “
- title2 = ‘It\’s So Nice’
- String Backslash Characters
- \\ = Include backslash
- \’ = Single quote
- \” = Double quote
- \n = New line
- Variables
- Variable = Name that refers to a value
- Limitations
- Cannot be a keyword (i.e. print, and, or, not)
- Cannot start with a number
- Case sensitive
- Cannot include illegal characters (i.e. $, %, +, =)
- Create variable names that mean something
- Bad: diy = 365
- Good: days_in_year = 365
- Math Operations
- Basic operations:
Add +, Subtract -, Multiply *, Divide /, Exponent **, Modulus %
- Order of precedence
- Parenthesis, Exponents, Multiply/Divide, Add/Subtract
- Left to right
- 2*(3-1) = 4
- 2*3-1=5
- String Operations
- Concatenation: Adding two strings together
- x = “Hello”
- y = “World”
- print x+y
- >>>Hello World
- Repetition: Repeating a string
- z = “Howdy”
- print z*3
- >>>Howdy Howdy Howdy
- Composition
- Composition is the ability to combine simple statements into
compound ones
- Can be used with commands
- x = “mud”
- print “Your name is:”, x
- >>>Your name is: mud
- Can be used with assignments
- average = (scores + extra_credit)/possible
- Comments
- Comments are notes you write to help explain what is going on in
the program
- Comments begin with #
- Everything after # is ignored by interpreter
- days=12 #Days available for project
- Use comments to help you remember
- First lines of codes should always have your name in a comment and
brief description of program
- Input
- For strings, use raw_input()
- name = raw_input(“What is your name? “)
- For numbers, use input()
- number = input(“How many? “)
- For both, write prompt in quotations inside the parenthesis.
The prompt is optional.
- Review
- Describe the following values:
Strings, integers, floating point, longs
- Why does the division below yield 0?
print 9/10
- Explain the guidelines for naming variables.
- What operations are available on strings?
- How are comments created and used?
- What is the difference between raw_input() and input()?
Restricted access |