Logistics:
Learning Objectives:
Variables are the same as variables in math, except math variables are often letters, but programming variables could be words.
Variable: a container that holds some information.
Note about variable declaration:
# Examples of variable declarations
width = 10
# Notice the "H" is capitalized
Height = 5
area = 0
width
# Expect an ERROR because the "height" variable is case-sensitive.
# ERROR CODE: "height" is not defined.
height
Height
# Using a python keyword as a variable name
# ERROR CODE: invalid syntax
global = 1
global
# More declarations for different variable types
# storing a string
helloMessage = "Hello World!"
first_name = "John"
# storing a char
character_example = 'a'
# storing a float
_newFloat = 1.0
# storing a boolean value
bool_Condition = True
helloMessage
character_example
_newFloat
bool_Condition
From Topic 1.1.1, we learned how to properly declare a variable name for different date types. In this topic, we will explore how to "cast" or convert data type between one another.
Helpful function: type() defines the type of the data
# From declaration above, width = 10 and 10 is an int, so we expects the function to return int
type(width)
type(helloMessage)
type(bool_Condition)
# Let's cast a float into an int and vice verse
# We will cast the type and the store it in a new variable
width_float = float(width)
type(width_float)
# Cast from float to int
width_int = int(width_float)
type(width_int)
# Cast between string and int
# Recall that width stores an int
# convert width to string
width_string = str(width)
type(width_string)
# convert width_string back to an int
type(int(width_string))
# Basic mathematical operations with Numbers
# Addition
print(5+23)
# Subtraction
print(100-25)
# Multiplication
print(5*10)
# Power/Exponent
# ** operator is equivalent to exponent
print(5**2)
# 5*5 = 5^2 = 5**2
print(5*5)
# Division (float)
# Return the actual decimal value of division
print(36/4)
print(10/3)
# Division (int)
# Return an int. If the actual quotient is a decimal value, only whole number is returned
print(10//3)
print(19//6)
# Modular Division: return the remainder of division
print(10%3)
# Operations with Strings and Characters
print("foo" * 5)
print('x'*3)
# ERROR: compiler treats x as a variable, not a character
print(x*3)
# ERROR: cannot concatenate an int to a string --> need to cast int to string
print("hello" + 5)
# Fix
print("hello " + str(5))
# String addition = concatenation
print("hello " + "world")
# Comparators: return boolean value
# Equality ==
# Note: MUST be Double equal signs, single equal sign is assignment
print(5 == 5.0)
# Greater than >
print(7 > 1)
# Less than <
print(1.5 < 90)
# Greater than or equal to >=
print(5.0 >= 5)
print(5.0 >= 4)
print(5 >= 13)
# Less than or equal to <=
print(10 <= 10.0)
print(10 <= 20)
print(8 <= 3)
# Comparators on Strings
print("hello" < "world")
print("hello" == "world")
print("hello" > "world")
print("hello" == "hello")
print("cat" < "dog")
Important Notices:
x = 7
y = 14
if (2*x == y):
print("y is double of x")
elif (x**2 == y):
print("y is the squared of x")
else:
print("y is NOT double of x")
x = 7
y = 49
if (2*x == y):
print("y is double of x")
elif (x**2 == y):
print("y is the squared of x")
else:
print("y is NOT related to x")
x = 7
y = 50
if (2*x == y):
print("y is double of x")
elif (x**2 == y):
print("y is the squared of x")
else:
print("y is NOT double nor squared of x")
Python does NOT have an implementation for the switch cases, but one way to implement the switch case is with the dictionary, a data structure that stores the key-value pair (Module 3).
def switcher(number):
# dictionary (from Module 3) to store switch cases
# If not found, then get() the default value
return {
'0':"Entered 0",
'1':"Entered 1",
'2':"Entered 2",
'3':"Entered 3",
'4':"Entered 4",
'5':"Entered 5",
'6':"Entered 6",
'7':"Entered 7",
'8':"Entered 8",
'9':"Entered 9",
}.get(number,"Invalid number!")
# input() reads in an user input from stdin
number = input("Dial a number")
switcher(number)
"""
EXERCISE: implement the above switch case example using if/else conditions
Prompt: For each digit between 0-9, the program will print a confirmation
for the entered value or print "invalid inputs" for all other numbers.
"""
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
DO NOT LOOK AT THE SOLUTION BELOW BEFORE YOU TRY THE EXERCISE!
number = input("Dial a number")
if number == '0':
print("Entered 0")
elif number == '1':
print("Entered 1")
elif number == '2':
print("Entered 2")
elif number == '3':
print("Entered 3")
elif number == '4':
print("Entered 4")
elif number == '5':
print("Entered 5")
elif number == '6':
print("Entered 6")
elif number == '7':
print("Entered 7")
elif number == '8':
print("Entered 8")
elif number == '9':
print("Entered 9")
else:
print("Invalid number!")