Python - Lab
Python - Lab
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Program #3 – Values and Types
Note: A value is one of the fundamental things-like a letter or a number-that a program manipulates.
A value could be a type of an integer, float, Boolean or string.
If you are not sure what type a value has an interpreter could tell you.
You can get data type of an object using type() function.
Values like '2' & "3.5", they look like numbers, but they are in quotation marks like strings.
Instructions:
❖ Write the following expressions on the Python IDLE.
>>> type(2)
<class 'int'>
>>> type(2.0)
<class 'float'>
>>> type('Hello, World!')
<class 'str'>
>>> type("Hello, World!")
<class 'str'>
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> type("3.5")
<class 'str'>
Program #4 – Variables
Note: A variable is a name that refers to (stores) a value.
A variable is container of a value.
A variable name should start with a letter or underscore (_).
A variable name cannot start with a number.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9 and _).
Variable names are case sensitive (name, Name and NAME are three different variables).
Although it’s legal to use uppercase letters, by convention we don’t.
If you give a variable an illegal name, you get a syntax error.
We use = sign to assign a value to a variable.
Python keywords cannot be used as variable names.
Keywords define the language’s rules and structure. Python has 33 keywords.
and def finally in or while
as del for is pass with
assert elif from import lambda raise yield
break else global None return
class except if nonlocal True
continue False import not try
Instructions:
❖ Write the following expressions on the Python IDLE.
>>> my_name = "Bereket"
>>> _age = 20
>>> more$ = 1000000
SyntaxError: invalid syntax
>>> name = "Selam"
>>> Name
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
Name
NameError: name 'Name' is not defined
>>> 76trombones = "big parade"
SyntaxError: invalid syntax
>>> class = "Programming"
SyntaxError: invalid syntax
Program #5 – Converting between data types
Note: You can change or set data type using int(), str(), bool() or float() functions.
bool() produces False for 0 or empty strings.
Instructions:
❖ Write the following expressions on the Python IDLE.
>>> int("52")
52
>>> float(9)
9.0
>>> bool(1)
True
>>> bool(0)
False
>>> bool("")
False
>>> bool("text")
True
>>> str(52)
'52'
Program #6 – Python built-in math commands (functions)
❖ Write the following expressions on the Python IDLE.
>>> abs(-5)
5
>>> max(20,30,10)
30
>>> min(15,5,20,2)
2
>>> sqrt(4)
2.0
>>> pi
3.141592653589793
>>> e
2.718281828459045
Program #7 – Python program that prompts (asks) the user for his/her amount of money, then
reports how many books with a price of 100 birr the person can afford, and how much more money
he/she will need to afford an additional book.
❖ Open Notepad or other editor and write the below code inside.
❖ Save the file with .py extension.
❖ Open the saved file with Python (You can do this from Python IDLE).
# imports math functions from Python's math module
from math import *
# calculates how many books the person could afford with the money
books_affordable = floor(amount_of_money / 100)
# calculates how much money is the person left with after buying books
Remainder = amount_of_money % 100
# calculates how much additional money the person needs to buy one more book
additional_money_needed = 100 - Remainder
2. Suppose the cover price (price on cover) of a book is $24.95, but bookstores get a 40% discount.
Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total
wholesale cost for 60 copies?
Solution:
original_price = 24.95
discounted_price = original_price * 0.4
total_copies = 60
print(total_cost)
3. Modify question #2, so that you ask the user for the number of copies.
Solution:
original_price = 24.95
discounted_price = original_price * 0.4
total_copies = int(input("How many copies do you want? \n > "))
print(total_cost)
a = int(input("a>"))
b = int(input("b>"))
c = int(input("c>"))
To solve that, we could either add a condition at the top level or define a function.
if a != 0:
{above conditions}
else:
print("a must be different from zero")
OR
def equationroots(a, b, c)
{above conditions}
if a == 0:
print("a must be different from zero")
else:
equationroots(a, b, c)