YCL Week 3 Lets talk about variables! Variables Variables are - - PowerPoint PPT Presentation
YCL Week 3 Lets talk about variables! Variables Variables are - - PowerPoint PPT Presentation
YCL Week 3 Lets talk about variables! Variables Variables are containers for data. Variables have two parts: a name and a value, which is of a certain data type. A variables value is usually assigned to it using an equal sign. x = 5
Variables
Variables are containers for data. Variables have two parts: a name and a value, which is of a certain data type. A variable’s value is usually “assigned” to it using an equal sign. x = 5 print(x)
Data Types
- 1. Numerical Types
- 1. Integers aka int: positive/negative whole number. Example: 5
- 2. floating-point : any number with a decimal point, most like decimals.
Example: 3.14
- 3. Long
- 4. Complex Numbers
- 2. Non-numerical types:
- 1. String: set of characters. Example: “Hello World!”
- 2. List: container that holds a number of other objects, in a given order.
- 3. Tuple
- 4. Dictionary(or "dict")
Naming Variables
Good Names:
- temperature_in_celsius
- tree_position
- car_speed
- number_of_children
- simpson
Legal, But Bad Names:
- temperatureInCelsius [Uses capital letters. Keep it lower case and use underscores.]
- x [Too short, and not descriptive.]
- Simpson [Starts with a capital letter.]
Illegal Names:
- tree position - you cannot use spaces
- 4runner - you cannot start with a number
Naming Example(s)
# Calculate mpg using confusing variable names m = 294 g = 10.5 m2 = m / g print(m2) # Calculate mpg using good variable names miles_driven = 294 gallons_used = 10.5 mpg = miles_driven / gallons_used print(mpg)
Constants
PI = 3.14159 SCREEN_WIDTH = 600 RED = (255, 0 ,0)
Operators
Operator Description + Addition
- Subtraction
* Multiplication / Division // Integer division (rounds down)
PEMDAS
- Parentheses
- Exponent
- Multiplication
- Division
- Addition
- Subtraction
Different from Math
There are two things that don’t work like you’d expect:
- 1. There is no “juxtaposition” used to multiply
items.
- 2. The = is not an algebraic equality.
= does not mean equal
# The last two lines will error x = 3 y = 2x z = 2(3 + x)
You can rewrite the code above to work by explicitly multiplying: # This code works. Although it doesn't print anything. x = 3 y = 2 * x z = 2 * (3 + x)
Different from Math
The = evaluates what is on the right, and puts it in the variable on the left. For example: # This works x = 3 + 4 x = 8 The code above does not signify equality; it is “assigning” the value of 7 to x. Later on, we could assign the value 8 to it, and x would print this new value. What would happen if we then assigned x a value of 9 and printed x, i.e. print(x)?
Counter Variables
x = 7 x = x + 1 print(x) What will this print?
calculator.py: Part 1
Write a program that prints out the results of at least one calculation for each of the basic
- perations: addition, subtraction, multiplication, division, and exponents. Use integers.
HINT: Type the word print and put whatever expression you would like to print in
- parentheses. This is a review of the print statement and introduces the printing of integers.
Remember, this is not a variable assignment (we are not assigning a value to anything). 1. Example solution: 1. print(6 + 4) 2. print(12 - 2) 3. print(2 * 5) 4. print(30 / 3)
calculator.py: Part 2
Store at least 4 integers into 4 different variables, and use these variables to perform
- perations within your print statement.
HINT: 1. Assign var1 a value of ‘3’. Do this by writing var1=3. 2. Assign variable 2 a value of ‘2’. Do this by writing var2=2. 3. Add the variables together, and print the result. Do this by writing print(var1+var2). 4. Save and run. What do you get?