0% found this document useful (0 votes)
40 views

Python Handout 2

Uploaded by

samibest769
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Python Handout 2

Uploaded by

samibest769
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Handout 2

Working with Data

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.

2. Variables can not contain spaces.

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.

Code Output Meaning


print(type(1)) <class ‘int’> It is an integer
print(type(1.1)) <class ‘float’> It is a float
print(type(False)) <class ‘bool’> It is a boolean
print(type(“False”)) <class ‘str’> It is a string

But how can we change a data from one type to another?

Data conversion Functions to use Meaning


Integer to String str(4) 4 is now a string
Float to string str(5.6) 5.6 is now a string
String to Integer int(“5”) 5 is now an integer
String to Float float(“5.068”) 5.068 is now a float
String to Boolean bool(“True”) True is now a boolean
Boolean to String str(False) False is now a string

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?

Consider this example.


first_name = “Abel”
last_name = “Kasu”
grade = 91.9
print(“Your name is ” + first_name + “ ” + last_name + “. And your grade is ” + grade + “.”)

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().

Consider this average calculator program.

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.

print(“Your average is: ” + str(average) + “.”) this will be error free.

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.

First_name = input(“Please enter your first name: ”)


Last_name = input(“Please enter your last name: ”)
print(“Hello ” + “ ” + First_name + “ ” + Last_name + “.”)

What will this code output?


On the first line, it prints the statement, Please enter your first name: and then it creates a space where
the user can write on. The second line of the code doesn’t run unless the user inputs something and
press enter.
So let’s assume the user enters the word, “Sir” inside and press enter. Now, the second line of the code
will run.
On the second line, lease enter your last name: will be printed. And then a space appears for the user
to write on. So let’s assume the user typed the word “Python” inside. Now the third line gets to run.
Python will now add the string Hello, First_name and Last_name with spaces in between the words and
a full stop at the end.
But what is First_name equal to? It is equal to whatever the user wrote in it. In this case it is Sir. And
Last_name is equal to Python.
So the last output will be, Hello Sir Python.

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?

Are inputs strings, integers, or boolean?


Inputs are strings. In the above example, we successfully added the string Hello with the input
First_name. Which must mean that First_name is also a string. Otherwise they wouldn’t be added. But
what if we want the user to input his/her grades?

Consider this example,


print(“I can add two numbers for you. Please enter them.”)
num1 = input(‘First number: ’)
num2 = input(‘Second number: ’)
print(“The sum of the two numbers is ” + num1 + num2)

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.”)

You might also like