0% found this document useful (0 votes)
18 views15 pages

Python Basics: Variables & Functions

The document provides a comprehensive guide to basic and advanced Python programming concepts, including variables, data types, control structures, functions, and object-oriented programming. It includes numerous exercises and challenges that encourage hands-on practice with Python syntax and logic. The advanced section covers topics like dictionaries, classes, and specific programming tasks such as calculating factorials and generating sequences.

Uploaded by

Stas Sh.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

Python Basics: Variables & Functions

The document provides a comprehensive guide to basic and advanced Python programming concepts, including variables, data types, control structures, functions, and object-oriented programming. It includes numerous exercises and challenges that encourage hands-on practice with Python syntax and logic. The advanced section covers topics like dictionaries, classes, and specific programming tasks such as calculating factorials and generating sequences.

Uploaded by

Stas Sh.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Part 1 – Python basic

# The lines that start with a hash (#) are comments


# They are for you to read and are ignored by Python

# When you see 'GO!', save and run the file to see the output
# When you see a line starting with # follow the instructions
# Some lines are python code with a # in front
# This means they're commented out - remove the # to uncomment
# Do one challenge at a time, save and run after each one!

# 1.1. This is the print statement

print("Hello world")

# GO!

# 1.2. This is a variable

message = "Level Two"

# Add a line below to print this variable

# GO!

# 1.3. The variable above is called a string


# You can use single or double quotes (but must close them)
# You can ask Python what type a variable is. Try uncommenting the next line:
# print(type(message))
# GO!

# 1.4. Another type of variable is an integer (a whole number)


a = 123
b = 654
c=a+b

# Try printing the value of c below to see the answer


# GO!

# 1.6. Variables keep their value until you change it

a = 100
# print(a) # think - should this be 123 or 100?

c = 50
# print(c) # think - should this be 50 or 777?

d = 10 + a - c
# print(d) # think - what should this be now?

# GO!

# 1.7. You can also use '+' to add together two strings

greeting = 'Hi '


name = '' # enter your name in this string

message = greeting + name


# print(message)

# GO!

# 1.8. Try adding a number and a string together and you get an error:

# age = # enter your age here (as a number)

# print(name + ' is ' + age + ' years old')

# GO!

# See the error? You can't mix types like that.


# But see how it tells you which line was the error?
# Now comment out that line so there is no error

# 1.9. We can convert numbers to strings like this:

# print(name + ' is ' + str(age) + ' years old')

# GO!

# No error this time, I hope?

# Or we could just make sure we enter it as a string:

# age = # enter your age here, as a string

# print(name + ' is ' + age + ' years old')

# GO!
# No error this time, I hope?

# 1.10. Another variable type is called a boolean


# This means either True or False

raspberry_pi_is_fun = True
raspberry_pi_is_expensive = False

# We can also compare two variables using ==

bobs_age = 15
# your_age = # fill in your age

# print(your_age == bobs_age) # this prints either True or False

# GO!

# 1.11. We can use less than and greater than too - these are < and >

# bob_is_older = bobs_age > your_age

# print(bob_is_older) # do you expect True or False?

# GO!

# 1.12. We can ask questions before printing with an if statement

money = 500
phone_cost = 240
tablet_cost = 200

total_cost = phone_cost + tablet_cost


can_afford_both = money > total_cost

if can_afford_both:
message = "You have enough money for both"
else:
message = "You can't afford both devices"

# print(message) # what do you expect to see here?

# GO!

# Now change the value of tablet_cost to 260 and run it again


# What should the message be this time?
# GO!

# Is this right? You might need to change the comparison operator to >=
# This means 'greater than or equal to'

raspberry_pi = 25
pies = 3 * raspberry_pi

total_cost = total_cost + pies

if total_cost <= money:


message = "You have enough money for 3 raspberry pies as well"
else:
message = "You can't afford 3 raspberry pies"

# print(message) # what do you expect to see here?

# GO!

# 1.13. You can keep many items in a type of variable called a list

colours = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet']

# You can check whether a colour is in the list

# print('Black' in colours) # Prints True or False

# GO!

# You can add to the list with append

[Link]('Black')
[Link]('White')

# print('Black' in colours) # Should this be different now?

# GO!

# You can add a list to a list with extend

more_colours = ['Gray', 'Navy', 'Pink']

[Link](more_colours)
# Try printing the list to see what's in it

# GO!

# 1.14. You can add two lists together in to a new list using +

primary_colours = ['Red', 'Blue', 'Yellow']


secondary_colours = ['Purple', 'Orange', 'Green']

main_colours = primary_colours + secondary_colours

# Try printing main_colours

# 1.15. You can find how many there are by using len(your_list). Try it below

# How many colours are there in main_colours?

# GO!

all_colours = colours + main_colours

# How many colours are there in all_colours?


# Do it here. Try to think what you expect before you run it

# GO!

# Did you get what you expected? If not, why not?

# 1.16. You can make sure you don't have duplicates by adding to a set

even_numbers = [2, 4, 6, 8, 10, 12]


multiples_of_three = [3, 6, 9, 12]

numbers = even_numbers + multiples_of_three


# print(numbers, len(numbers))
numbers_set = set(numbers)
# print(numbers_set, len(numbers_set))

# GO!

colour_set = set(all_colours)
# How many colours do you expect to be in this time?
# Do you expect the same or not? Think about it first

# 1.17. You can use a loop to look over all the items in a list
my_class = ['Sarah', 'Bob', 'Jim', 'Tom', 'Lucy', 'Sophie', 'Liz', 'Ed']

# Below is a multi-line comment


# Delete the ''' from before and after to uncomment the block

'''
for student in my_class:
print(student)
'''

# Add all the names of people in your group to this list

# Remember the difference between append and extend. You can use either.

# Now write a loop to print a number (starting from 1) before each name

# 1.18. You can split up a string by index

full_name = 'Dominic Adrian Smith'

first_letter = full_name[0]
last_letter = full_name[19]
first_three = full_name[:3] # [0:3 also works]
last_three = full_name[-3:] # [17:] and [17:20] also work
middle = full_name[8:14]

# Try printing these, and try to make a word out of the individual letters

# 1.19. You can also split the string on a specific character

my_sentence = "Hello, my name is Fred"


parts = my_sentence.split(',')

# print(parts)
# print(type(parts)) # What type is this variable? What can you do with it?

# GO!

my_long_sentence = "This is a very very very very very very long sentence"

# Now split the sentence and use this to print out the number of words

# GO! (Clues below if you're stuck)


# Clue: Which character do you split on to separate words?
# Clue: What type is the split variable?
# Clue: What can you do to count these?

# 1.20. You can group data together in a tuple

person = ('Bobby', 26)

# print(person[0] + ' is ' + str(person[1]) + ' years old')

# GO!

# (name, age)
students = [
('Dave', 12),
('Sophia', 13),
('Sam', 12),
('Kate', 11),
('Daniel', 10)
]

# Now write a loop to print each of the students' names and age

# GO!

# 1.21. Tuples can be any length. The above examples are 2-tuples.

# Try making a list of students with (name, age, favourite subject and sport)

# Now loop over them printing each one out

# Now pick a number (in the students' age range)


# Make the loop only print the students older than that number

# GO!

# 22. Another useful data structure is a dictionary

# Dictionaries contain key-value pairs like an address book maps name


# to number

addresses = {
'Lauren': '0161 5673 890',
'Amy': '0115 8901 165',
'Daniel': '0114 2290 542',
'Emergency': '999'
}

# You access dictionary elements by looking them up with the key:

# print(addresses['Amy'])

# You can check if a key or value exists in a given dictionary:

# print('David' in addresses) # [False]


# print('Daniel' in addresses) # [True]
# print('999' in addresses) # [False]
# print('999' in [Link]()) # [True]
# print(999 in [Link]()) # [False]

# GO!

# Note that 999 was entered in to the dictionary as a string, not an integer

# Think: what would happen if phone numbers were stored as integers?

# Try changing Amy's phone number to a new number

# addresses['Amy'] = '0115 236 359'


# print(addresses['Amy'])

# GO!

# Delete Daniel from the dictinary

# print('Daniel' in addresses) # [True]


# del addresses['Daniel']
# print('Daniel' in addresses) # [False]

# GO!

# You can also loop over a dictionary and access its contents:

'''
for name in addresses:
print(name, addresses[name])
'''

# GO!
# 1.23. A final challenge using the skills you've learned:
# What is the sum of all the digits in all the numbers from 1 to 1000?

# GO!

# Clue: range(10) => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


# Clue: str(87) => '87'
# Clue: int('9') => 9

1.24. Define a function `max()` that takes two numbers as arguments and
returns the largest of them. Use the if-then-else construct available in Python.
(It is true that Python has the `max()` function built in, but writing it yourself is
nevertheless a good exercise ).

1.25. Define a function `max_of_three()` that takes three numbers as


arguments and returns the largest of them.

1.26. Define a function that computes the length of a given list or string. ( It is
true that Python has the `len()` function built in, but writing it yourself is
nevertheless a good exercise ).

1.27. Write a function that takes a character ( i.e. a string of length 1 ) and
returns `True` if it is a vowel, `False` otherwise.

1.28. Write a function `translate()` that will translate a text into "rövarspråket"
(Swedish for "robber's language"). That is, double every consonant and place
an occurrence of "o" in between. For example, `translate("this is fun")` should
return the string `"tothohisos isos fofunon".`

Part 2 - advanced

2.1
Write a program which will find all such numbers which are divisible by 7 but
are not a multiple of 5,
between 2000 and 3200 (both included).
The numbers obtained should be printed in a comma-separated sequence on
a single line.

Hints:
Consider use range(#begin, #end) method
2.2
Write a program which can compute the factorial of a given numbers.
The results should be printed in a comma-separated sequence on a single
line.
Suppose the following input is supplied to the program:
8
Then, the output should be:
40320

Hints:
In case of input data being supplied to the question, it should be assumed to
be a console input.

2.3
With a given integral number n, write a program to generate a dictionary that
contains (i, i*i) such that is an integral number between 1 and n (both
included). and then the program should print the dictionary.
Suppose the following input is supplied to the program:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

Hints:
In case of input data being supplied to the question, it should be assumed to
be a console input.
Consider use dict()

2.4
Write a program which accepts a sequence of comma-separated numbers
from console and generate a list and a tuple which contains every number.
Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')

Hints:
In case of input data being supplied to the question, it should be assumed to
be a console input.
tuple() method can convert list to tuple
2.5
Write a method which can calculate a square value of a number
2.6
Python has many built-in functions, and if you do not know how to use it, you
can read document online or find some books. But Python has a built-in
document function for every built-in functions.
Please write a program to print some Python built-in functions documents, such
as abs(), int(), raw_input()
And add document for your own function

Hints:
The built-in document method is __doc__
2.7.
class
Answer the follwing questions and write the following classes
1. What is a class?
2. What is an instance?
3. What is encapsulation?
4. What is inheritance?
5. What is polymorphism?
6. Traingle Class:
a. Create a class, Triangle. Its __init__() method should take self,
angle1, angle2, and angle3 as arguments. Make sure to set these
appropriately in the body of the __init__()method.
b. Create a variable named number_of_sides and set it equal to 3.
c. Create a method named check_angles. The sum of a triangle's
three angles is It should return True if the sum of self.angle1,
self.angle2, and self.angle3 is equal 180, and False otherwise.
d. Create a variable named my_triangle and set it equal to a new
instance of your Triangle class. Pass it three angles that sum to 180
(e.g. 90, 30, 60).
e. Print out my_triangle.number_of_sides and print out
my_triangle.check_angles().
7. Songs class:
a. Define a class called Songs, it will show the lyrics of a song. Its
__init__() method should have two arguments:selfanf lyrics. Lyrics is
a list. Inside your class create a method called sing_me_a_songthat
prints each element of lyrics on his own line. Define a varible:
b. happy_bday = Song(["May god bless you, ",
"Have a sunshine on you,",
"Happy Birthday to you !"])

c. Call the sing_me_song method on this variable


8. Define a class called Lunch. Its __init__() method should have two
arguments:selfanf [Link] menu is a string. Add a method called
menu_price.It will involve a ifstatement:
if "menu 1" print "Your choice:", menu, "Price 12.00", if "menu 2" print
"Your choice:", menu, "Price 13.40", else print "Error in menu". To check
if it works define: Paul=Lunch("menu 1") and call Paul.menu_price().
9. Define a Point3D class that inherits from object Inside the Point3D class,
define an __init__() function that accepts self, x, y, and z, and assigns
these numbers to the member variables self.x,self.y,self.z. Define a
__repr__() method that returns "(%d, %d, %d)" % (self.x, self.y, self.z).
This tells Python to represent this object in the following format: (x, y, z).
Outside the class definition, create a variable named my_point containing
a new instance of Point3D with x=1, y=2, and z=3. Finally, print
my_point.

You can find the solutions to these exercises in the link:


[Link]
free/classes/[Link]

2.8.
List comprehension - a quick reminder
1. Use dictionary comprehension to create a dictionary of numbers 1- 100 to
their squares (i.e. {1:1, 2:4, 3:9 …}
2. Use list comprehension to create a list of prime numbers in the range 1-
100
[Link]
2.9.
Files
Use open, read, write, close which are explained in these tutorials:
[Link]
[Link]
to:
1. Write “Hello world” to a file
2. Read the file back into python.
3. Write the square roots of the numbers 1-100 into the file, each in new
line.
2.10
With
Read about with in one of the following links:
[Link]
with-used-for
[Link]
1. Open a file using with and write the first paragraph from (you don’t need
to read the webpage, just copy paste it into code)
[Link]
2. Open the file using with and read the contents using readlines.

2.11.
Strings
1. Split the paragraph to a list of words by spaces (hint: split())
2. Join the words back into a long string using “join”. (hint: join())
* (optional) Create a paragraph with the word order reversed in each
sentence (but keep the order of the sentences)
3. write a function accepting two numbers and printing “the sum of __ and
___ is ___”.
Do this twice using one string formats learned in class each time.
Hint: [Link]

2.12.
Datetime, time, timeit
Read the current datetime to a variable usint [Link] see:
[Link]
10. Print the date of the day before yesterday, and the datetime in 12 hours
from now
Use time library to time library to time operations by storing [Link]()
before and after running an operation
[Link]
1. Time one of the comprehension exercises you’ve performed and compare
to a simple for implementation, to determine which one is faster

1. Now use timeit library to time your list comprehension operation, see:
[Link]

2.13.
Exceptions
Read about exceptions from:
[Link]
python
1. Enhance the function you wrote which reads numbers (Strings 3.)
a. In case of a string input writing an error message instead of raising
an error
b. Raise an error in case one of the input numbers is > 100
c. Create a code which tries to read from the dictionary of squares
created

2.14.
Logger
Read about loggers from the example:
[Link]
Create a logger in any of your programs and log various messages to a file
using a FileHandler, and to a the console using StreamHandler
*Optional : try setting different levels (e.g. debug for console and info for file)
and different formats and try different levels of logs in your code.
2.15
Regular expressions (re)
You can read about regex from the following sources:
Python documentation - [Link]
General links about RegEx - [Link]
And - [Link]
(The last is more brief and practical)
Try to understand the difference between search and match (and compile),
and how to match the strings exactly.
1. To get the hang of it, try to do a few of the the exercises in:
[Link]
2. To get a sense of regex in python, take a look at the first few exercises in:
[Link]
3*. Optional: Write a regular expression for identifying email addresses,
find and compare to regular expression found online.

2.16.
Combine everything!
Write a simple “range calculator” function which reads a file and writes the
result into another file. You should log the operations, and how much time
they took and handle problematic inputs (strings, wrong characters, etc.).
Validate the input using regex.
The file will contain in each line two numbers denoting a range of numbers
and they can only be integers, an operation denoted by a sign (+,*,-,/,**) and
a third number which will be applied with the operation for each of the
numbers in the range. Note all numbers and signs are separated by blanks
(please don’t use .csv readers and use plain readers):
5 100 - 17
18 25 * 2.784
(First line: subtract 17 from all numbers between 5 and 100, Second line:
multiply by 2.784 all numbers between 18 and 25)

The result of each range should be written into one line, separated with
commas and with the precision of two digits after the decimal point.

2.17.
os
Os module provides a portable way of using operating system dependent
functionality you can read about os in this link.
[Link]

1. print absolute path on your system


2. print files and directories in the current directory on your system
2.18.
glob.
The glob module finds all the pathnames matching a specified pattern
according to the rules used by the Unix shell.
You can read about it in the next link.
[Link]
Find in your download directory all the files that have extension pdf (.pdf)

2.19.
enumerate
[Link]
Write a list with some values, use for and enumerate to print in each iteration
the value and his index

2.20.
Threads

1. Write two functions, one that writes “SVD” article from wikipedia to a file
and one that calculates the sum of two numbers. Run the two functions
simultaneously using threads.
2. Use [Link]() in order to acquire and release mutexes so the two
functions don’t run simultaneously.
Hint: [Link]

2.21

Random

[Link]

Create a list with 10 values (string)

Take only one value from the list from a random place

Split the list to two lists in different sizes each list contain values from the
previous list but in random order.

You might also like