print(type(10))
print(type("abc"))
print(type(print))<class 'int'>
<class 'str'>
<class 'builtin_function_or_method'>
We have already encountered three types of Python objects:
10, -5, 0"hello" or 'world'print functionWe can tell the type of any object with the type function.
Exercise 1 (with Gemini): Ask Gemini to “write Python code that prints the type of the number 3.14”
Exercise 2 (on your own): Type print(type(100)) in a code cell and run it.
Numbers with decimal points are a different type called floating point objects, or floats.
They require more space in memory to store than integers.
Exercise 1 (with Gemini): Ask Gemini to “write Python code that creates a variable with a decimal number and prints its type”
Exercise 2 (on your own): Type print(5.0 + 3.0) and run it.
Standard mathematical operations work on ints and floats:
+)-)*)**) - note: this is ** in Python, not ^ like in ExcelExercise 1 (with Gemini): Ask Gemini to “write Python code that calculates 5 to the power of 3”
Exercise 2 (on your own): Type print(8 * 7) and run it.
Python has three types of division operators:
/) - returns a float//) - returns the largest integer ≤ result%) - returns the remainderExercise 1 (with Gemini): Ask Gemini to “write Python code that shows the remainder when 17 is divided by 5”
Exercise 2 (on your own): Type print(20 // 6) and run it. What is the result?
Strings are sequences of characters enclosed in quotes.
You can use single or double quotes - just be consistent at the beginning and end.
We can concatenate (paste) strings together with +:
Exercise 1 (with Gemini): Ask Gemini to “write Python code that concatenates ‘Hello’ and ‘World’ with a space between them”
Exercise 2 (on your own): Type print("Python" + "!") and run it.
Booleans represent True or False values.
Basic comparison operators:
> (greater than)< (less than)>= (greater than or equal to)<= (less than or equal to)== (is equal to)Important: Use two equals signs (==) for comparison. A single = is for assignment.
Exercise 1 (with Gemini): Ask Gemini to “write Python code that checks if 15 is greater than or equal to 10”
Exercise 2 (on your own): Type print(7 < 5) and run it. Is the result True or False?
We can combine booleans with logical operators:
and - A and B is True only if both are Trueor - A or B is True if either (or both) is TrueExercise 1 (with Gemini): Ask Gemini to “write Python code that uses ‘and’ to check if a number is between 10 and 20”
Exercise 2 (on your own): Type print(True or False) and run it.
Python provides functions to convert between types:
int() - converts to integer (truncates, doesn’t round)float() - converts to floating pointstr() - converts to stringExercise 1 (with Gemini): Ask Gemini to “write Python code that converts the string ‘100’ to an integer”
Exercise 2 (on your own): Type print(str(999)) and run it.
Python objects have attributes and methods:
Access them using a period (.) after the object:
Strings have many useful methods!
Useful string methods:
.upper() - converts to uppercase.lower() - converts to lowercase.strip() - removes whitespace from ends.replace(old, new) - replaces textExercise 1 (with Gemini): Ask Gemini to “write Python code that converts ‘python programming’ to uppercase”
Exercise 2 (on your own): Type print("hello".replace("h", "j")) and run it.
f-strings (formatted strings) let us insert variables into strings.
Syntax: f"text {variable} more text"
Exercise 1 (with Gemini): Ask Gemini to “write Python code using an f-string to print ‘My name is Bob and I am 30 years old’”
Exercise 2 (on your own): Type name = "Sam" then print(f"Hello, {name}!") and run it.
We can format numbers in f-strings:
.2f - floating point with 2 decimal places.0% - convert decimal to percentage:, - add commas to large numbersExercise 1 (with Gemini): Ask Gemini to “write Python code that formats the number 0.75 as a percentage”
Exercise 2 (on your own): Type num = 1234567 then print(f'{num:,}') and run it.
. notation to access attributes and methodsNext up: Lists and more complex data structures!