2 + 3
5 * 7
9 - 2
15 / 5
2**33 Operators, Variables, Boolean Expressions, and Datatypes
3.1 Basic math operators
What is the output of the following?
3.2 Defining variables
What is the value of \(y\)?
\[ y = \frac{2(5^2) + 3(5) + 1}{4(5) - 5}\]
# Calculate the value of y
y = (2 * (5**2) + 3 * 5 + 1) / (4 * 5 - 5)
# Output the result
print(f"The value of y is {y}")The value of y is 4.4
3.3 Creating variables
Some rules for creating variables are:
- Must start with a letter or an underscore.
- The rest of the name can include letters, numbers, and underscores.
- Variable names are case sensitive ‘age’ vs ‘Age’ vs ‘AGE’ are all different
- Avoid using the word Python or function names.
- Others?
Examples:
What is the output of the following?
pi = 3.14159
radius = 2.2
# area of circle equation <- remember this is a comment
area = pi*(radius**2)
print(area)15.205295600000001
3.5 Autocomplete
# Spyder can autocomplete names for you
# start typing a variable name defined in your program and hit tab
# before you finish typing -- try it below
# define a variable
a_very_long_variable_name_dont_name_them_this_long_pls = 0
# below, start typing a_ve then hit tab... cool, right!
# use autocomplete to change the value of that variable to 1
# use autocomplete to write a line that prints the value of that long variable
# notice that Spyder also automatically adds the closed parentheses for you!3.6 Datatypes: Strings (text types)
#hi = "hello there"
#name = "ana"
#greet = hi + name
#print(greet)
#greeting = hi + " " + name
#print(greeting)
#silly = hi + (" " + name)*3
#print(silly)hi = "hello there"
name = "ana"
greet = hi + name
print(greet)
greeting = hi + " " + name
print(greeting)
silly = hi + (" " + name)*3
print(silly)3.7 Output and input
Output:
#x = 1
#print(x)
#x_str = str(x)
#print("my fav number is", x, ".", "x=", x)
#print("my fav number is", x_str + "." + "x=" + x_str)
#print("my fav number is" + x_str + "." + "x=" + x_str)Input:
#text = input("Type anything... ")
#print(5*text)
#num = int(input("Type a number... "))
#print(5*num)3.8 Comparison operators
The “=” operator is used to assign a value to a variable.
x = 5 # Assigns the value 5 to the variable x.The “==” operator is used to check if two values are equal.
if x == 5:
print("x is 5") # This line executes if x equals 5.x is 5
The “!=” operator checks if two values are not equal.
if x != 5:
print("x is not 5") # This line executes if x is not equal to 5.How would you code greater than 5? Less than 5? Less than or equal to 5? Greater than or equal to 5?
3.9 Modulo operations:
Modulo Operation: When you divide one number by another, the modulo operation gives you the remainder from that division. In Python, the % sign is used as the modulo operator. It returns the remainder of a division between two numbers.
# What would be the output of the following?
# notice the spaces do not matter here
7 % 5
7%52
10 % 20
9 % 41
You can use the modulo operation to check if a number is even or odd. For example, x % 2 will return 0 if x is even, and 1 if x is odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("number is even")
else:
print("number is odd")3.10 Boolean expressions
What are boolean expressions?
Boolean expressions in Python are expressions that evaluate to either True or False.
Examples:
What is the output of the following?
x = 5
y = 10
print(x == y)
print(x < y)False
True
a = True
b = False
print(a and b)
print(a or b)
print(not a) False
True
False
3.11 Datatypes: Numeric
print(type(342)) # this is an integer
print(type(342.34)) # this is a float
# A float is a real number, but not all real numbers are floats<class 'int'>
<class 'float'>
Examples:
What would be the numeric types for each of the following?
x = -20*5y = -20/5y = -20/83.12 Exercises
Why do we use comments in Python, and what makes a good comment? How do you write one?
What is a Boolean expression, and what kind of result does it produce?
What does the type() function do in Python? When would you use it?
Maya has 36 pencils and wants to divide them evenly into 4 boxes. She uses Python to calculate how many pencils go in each box using the / operator. What line of code would she use, and what would the result be?
What would change in the previous question if Maya wanted to have 5 boxes? How would that affect your result and how would you change your code if she says she wants the answer rounded down to the nearest whole number (without using round())?
Kyle has 18 candies and wants to divide them equally among 4 people. How many will be left over? Write a line of Python code to calculate the number of candies left over after the division.
What does the += operator do? Why is it useful?
What line of code would you use to calculate 2 raised to the power of 6? What is the output?
Look at the following Python code that uses math operations:
x = 36 / 4 * (3 + 2) * 4 + 2
print(x)What is the output and how does Python decide the order of operations?
- Ali tries to shorten his code using:
y = 10
x = y += 2
print(x)But Python gives him an error. Why does this happen, and how should he fix it?
What’s the difference between a syntax error and a logic error in Python?
What’s the advantage of using f-strings instead of traditional string concatenation?
Using an f string, generate the following output: “Hello, Tom. You are 2 years old.” Hint: Define the name and age variables first.
What will this code print and why?
a = "Java"
b = "Script"
a += b
print(a)Write a Python program that accepts the user’s first and last name and prints them in reverse order with a space between them.
Why does this code produce an error, and how can it be fixed?
var1 = 1
var2 = 2
var3 = "3"
print(var1 + var2 + var3)What does the * operator do when used with strings?
Lucy wrote this code to repeat a name:
var = "James" * 2 * 3
print(var)What is the output and how is the multiplication applied?
What does the modulo (%) operator return compared to / and //?
Write a Python code that can check if a number is even or odd.
Xinya is writing a Python program to display a quiz score. She tries this line of code:
print("The result is: " + 10)But she gets an error. What’s wrong with this code, and how can she fix it?
- What’s wrong with this code, and how would you fix it?
num = "5"
total = num + 10
print(total)Explain the difference between ‘=’ and ‘==’.
What’s wrong with this code and how would you fix it?
x = 7
if x = 5:
print("x is five")- Jordan is checking if two locker codes are different. The first code is 3 and the second is 5. He uses Python to compare them. What is the logic behind his code below and what does it output?
a = 3
b = 5
print(a != b)
3.4 Comments