Python Handout 2
Python Handout 2
Lesson One
Variables
What are variables?
Variables in Python are just like the variables we know in mathematics. They are placeholders that can
store any data. They can be assigned by the symbol, =. These values can be strings, boolean, integer,
floats, or any other data types.
The ‘=’ operator is known as the assignment operator. It is not the same as ‘==’ which mean is equal to.
X=5
print(X) gives us the output, 5.
number = 5.6
print(number) will give us the output, 5.6.
FirstName = “Adonias”
print(FirstName) will give us the output, Adonias.
IsGrounded = False
print(isGrounded) will give us the output, False.
Naming of variables.
1. Variables must start with a letter or an underscore(_). They can not start with numbers and other
symbols.
3. Variables should not have the same name as reserved key words like, print, type, input, if, for, while,
else, etc…
4. It is good(but not must) to make your variables descriptive of their values. For example, first_name
is a better variable than X, if you’re assigning a person’s name. And, ‘age’ is a better variable than
‘num’ if you’re assigning a person’s age.
We can use the operations and comparisons on variables just like how we used to in the past.
num1 = 1
num2 = 2
print(num1 + num2) gives the output, 3.
num1 = 6
num2 = 4
print(num1 // num2) gives the output, 1.
Review
1. What is the difference between num = 3 and num == 3? And which one is a variable?
___________________________________________________________________________________
2. What is the output of the following code?
a. num1 = 5
num2 = 5
print(num1 // num2)
b. num1 = “5”
num2 = “2”
print(num1 // num2)
c. num1 = 5
num1 = 2
num1 = 5 // 2
print(num1)
3. Which of the following variables are valid? To the ones that are not valid, which rule do they break?
a. Firstname
b. FirstName
c. First_Name
d. First Name
e. _FirstName
f. F%stName
g. FirstName%
h. %FirstName
I. FirstName3
j. 3FirstName
k. input
l. if
m. type
Lesson Two
Data Conversion
The type() function allows us to determine what data type does the data inside the brackets have. Don’t
forget that we need to type the print function before the type function if we want to display the result on
our screen.
Data conversion is a very important thing in programming. It is used in so many programs. We will see
them one by one in the future. For now consider this example,
Let’s say we want to write a code that adds a person’s first name and last name together and outputs it.
first_name = “Abel”
last_name = “Kasu”
print(“Your name is ” + first_name + “ ” + last_name + “.”)
This code gives the output: Your name is Abel Kasu. But what if we want to add another variable that is
an integer?
This would result in an error because we can’t add the integer, grade, with the rest of the strings.
Remember we can only add strings with strings.
There are many ways to fix these errors.
1. putting 91.9 inside quotation marks.
2. converting grade into a string by using the function str().
Amharic = 93
English = 88
Math = 99
Science = 95
Programming = 99
total = Amharic + English + Math + Science + Programming
average = total / 5
print(“Your average is: ” + average + “.”)
This will result in an error. And this time, we can’t fix that by putting all the integers in strings.
Because if we do, the total will be 9388999599 (because of concatenation) which is not what we want.
So what we should do is convert average to a string.
Review
1. What is the output of the following codes?
a. print(type(“1234’))
b. print(type(‘ ’))
c. print(type(762.5 + 485 + 384))
d. print(type(0))
e. print(type(7 > 8 > 9))
f. print(type(2 < 9 == 9.0))
g. print(type(True))
h. print(type(str(1))
i. print(type(str(1.1))
j. print(type(int(“1”))
k. print(type(float(“1”))
2. Create three variables called, num1, num2, and remainder. Write a function that outputs the
remainder of two numbers and print them in the format, the remainder of num1 divided by num2 is
remainder. For example, if num1 = 5 and num2 = 3, the output should be, The reminder of 5 divided by
3 is 2.
Lesson Three
User Input
Input can be taken from the user of your program by the function, input(). input() creates a space in the
console of your program and users can type anything on it.
input() this code creates a places that users can write in.
input(“Please enter your age: ”) this code prints out the statement “Please enter your age: ” and then
creates a space for users to write in.
input(“Are you ready? Reply with yes or no. ”) this code prints out the statement, “Are you ready?
Reply with yes or no. ” first and then creates a space for users to write in.
We can assign inputs to variables.
Consider this example.
Note that,
print(“Hello ” + “ ” + First_name + “ ” + Last_name + “.”)
print(“Hello ” + “ ” + “First_name” + “ ” + “Last_name” + “.”)
These two lines are not the same, the first one outputs, Hello Sir Python. The second one outputs, Hello
First_name Last_name. Can you guess why?
Let’s assume the user inputs 3 and 5 respectively. What will the last line output? Is it The sum of the
two numbers is 8 or The sum of the two numbers is 35? Well, num1 and num2 are inputs so they are
strings. And strings can be concatenated not added. Which means num1 + num2 will give us, 35. Not 8.
So how can we fix this unwanted result? We can convert num1 and num2 to integers by the int()
function. print(“The sum of the two numbers is ” + int(num1) + int(num2)) will give the output, The
sum of the two numbers is 8.
Review
1. Create a variable called email and assign in to an input function. The user should be able to write
their email account after the statement, “Please enter your email here: ”
2. Create a program that multiplies all inputs from the user.
3. What is the output of this code?
user_interest = input(“Do you like Python? ”)
print(“Thank you for your answer.”)