Previous Lecture Lecture 2 Next Lecture

Lecture 2, Thu 01/11

Basic Input / Output, Variables and Assignments, Python Data Types

Recorded Lecture: 1_11_24

Print Functions

print("Hello CSW8!")
print("Python is amazing!")
print("Hello CSW8!\nPython is amazing!")
print("Hello CSW8!", "Python is amazing!", "Keep the strings going!")
print("Hello CSW8!", end="") # note: "" is an empty string
print("Python is amazing!")
print("Hello CSW8!", "Python is amazing!", "Keep the strings going!", sep="")

Python Data Types

Some Common Python Data Types

type() Function

print(type(1)) #int
print(type("1")) #str
print(type(1.0)) #float

Some Numerical Operators

print(2 + 2) #4 (int)
print(2 + 2.0) #4.0 (float)
print(2 - 1) #1 (int)
print(2 - 1.0) #1.0 (float)
print(2 * 3) #6 (int)
print(2 * 3.0) #6.0 (float)
print(1 / 2) #0.5 (float)
print(2 / 2) #1.0 (float - doesn't matter if using only ints)
print(2 ** 3) #8 (int)
print(2 ** 3.0) #8.0 (float)
print(10 // 3) #3 (int)
print(10.0 // 3) #3.0 (float)
print(10 % 3) #1 (int)
print(10.0 % 3) #1.0 (float)

Variables and Assignments

x = 10
print(x)
x = x * 10 #10 * current value of x replaces existing value of x
print(x) #100

Another Note on Operators and Types

print("CSW" + "8") #CSW8 (concatenation)
print("CSW" + 8) #ERROR!

Conversion Functions

x = "10.0"
print(type(x)) # string type

x = float(x)
print(type(x)) # float type

x = int(x) #OK, removes decimal portion from float number
print(type(x)) # int type

x = str(x)
print(type(x)) # string type

# When converting a string to an int, it doesn't expect the decimal portion
# in the string
x = "10.0"
x = int(x) # Error

Input Function

TAX_RATE = 0.1
userName = input("Hi, please enter your name: ")

print("Hi,", userName, ". What’s the amount of your bill (not including tax and tip)?")
totalBill = float(input()) #take the input() string and convert it to a float.

print("What tip percentage would you like to leave?")
tipPercentage = float(input())

taxAmount = totalBill * TAX_RATE
tipAmount = totalBill * (tipPercentage / 100)
print("The total amount to pay is $", totalBill + taxAmount + tipAmount)