I'm not going to reinvent the wheel so this introduction to Python will be based on already existing and great tutorials available on web. At the beginning I have to also emphasise that some concepts and ideas are explained more on offline workshops by me, so just keep in mind that content available here is more of a reminder than a learning material per se. This introduction is (without bigger changes) bases on this introduction to Python3.
And so, without further ado, let's begin!
Python was created by Guido Van Rossum in the early 90s. It is now one of the most popular languages in existence, and definitely one of the most fun to code in. We will go through fundamentals and try to solve exercises as we progress through.
- Primitive Datatypes and Operators
- Variables and Collections
- Control Flow and Iterables
- Functions
- Modules
- Classes
- Advanced
Exercises are in order that should represent progress of each section. You can read modules first and then try to solve exercises, or the other way. It's up to you. Just keep in mind that not always everything covered by modules will be sufficient to solve problems listed below. Extra sections are short exercises that relate to problem but try to emphasise some key concepts that might be overlooked at first sight.
Because each and every programmer loves when things get big really fast. It's time to do so.
- Write
rec_factorial
function using simple recursion. - Write
iter_factorial
function using while or for loop. - Write
list_factorial
function that will return list of first n factorials inclusive
Extra:
- Try to break your code by passing an invalid value to functions. What happens, and why? What can you do to avoid this kind of errors?
- Well, factorial of 5 is only 3 digits wide, how about 100 factorial? There should be an overflow, shouldn't it? Just like in C or Java. What happens?
list_factorial
can be written in at least two ways. Which one is more efficient and why?- It is possible to write write factorial function in recursive form but using accumulator. Try to write
acc_factorial
What about all time favourite programming assignment? Let's see how difficult it will be using Python.
- Write
rec_fibonacci(n)
function using simple recursion. - Write
acc_fibonacci(n)
function using recursion with accumulator. - Write
iter_fibonacci(n)
function using while or for loop. - Write
list_fibonacci(n)
function that will return list of first n fibonacci numbers inclusive.
Extra:
- Try to break your code by passing an invalid value. Did you implement function in such way that it won't break when passed variable is string literal?
- There is one other way to calculate fibonacci number. Can you implement it
root_fibonacci
? (hint: sqrt(5) ) Can you write another function to check for which n the difference between exact value and calcualted by this function is greater than some given delta?
What's not to love in prime numbers? I don't know either...
- Write
is_prime(n)
function in best known way. - Write
erastotenes(n)
function that return list of only prime numbers from set of 0 to n (exclusive).
Extra:
- Try to break your code. What about negative integers?
- Look at
range
function and check if you can use its full potential in iterating over sequences by given step. - Look at
map
function. Write functiononly_primes([int])
that filters input list and returns list of only prime numbers from it. - There is a probabilistic way to check if number is prime. Try to implement Miller-Rabin primality test (
miller_rabin_test
).
We all love books, don't we? Well if you don't, you should at least pretend to do so...
- Write function
most_used(file, count)
that reads text file from input and returns list of count most used words from file and number of its occurrences. Let's assuble that we split text into words on whitespaces only. (hint: dictionaries and sorting lists might be useful)
Extra:
- Can you write this code in fewer lines?
- Using appropriate module you can define list of characters that will represent word separators (default
.split()
works on whitespaces). Modify your program so that words will be split on each character from defined list.
Well, I probably won't remember yours... :(
- Create dictionary
friends
wherekey == name && value == special_trade
of all your friends. - Create list
friend_names
that only contains only names of your friends. - Create list
special_trades
that contains only those trades that no one else has. - Create new dictionary that is
name: [trades]
. - Each person whom name starts with letter A gets new
awesome
trade.
Extra:
a = ["Awesome", "Awesomeness"]
b = [a for _ in range(3)]
b.append(["Awesome", "Awesomeness"])
a.append("End")
print(b)
I know it's not mandatory course, but hey! I need to know if you understand anything so far. :)
# Dictionary that represents students from last class.
# Key: unique id, Value: (name, semester, list of favorite OS's)
students = {
0: ("Adam", 7, ["Linux", "Windows 10", "Mac OS"]),
1: ("Konrad", 3, ["Linux"]),
2: ("Monika", 3, ["Linux", "Windows"]),
3: ("Piotrek", 3, ["Linux"]),
4: ("Kuba", 3, ["Mac OS"]),
5: ("Krzysiek", 3, ["Linux"]),
6: ("Krzysiek", 3, ["Windows 10"]),
7: ("Adam", 1, ["Windows 10"]),
8: ("Marcel", 1, ["Windows 10"]),
9: ("Paulina", 1, ["Windows 10"]),
10: ("Sebastian", 1, ["Windows 10"])
}
- Copy dictionary from above into your python file (module). I apologise if some data is incorrect, my memory is not perfect :)
- Write function
students_count(students)
that returns number of students in class - write function
was_present(students, name)
that returnsTrue
only if someone of givenname
was on last class. - Write function
unique_was_present(students, unique_id)
that returnsTrue
only if someone of givenunique_id
was on last class. - Write function
get_all_by_name(students, name)
that returns sub-dictionary of students of given name. - Write function
get_all_by_semester_range(students, sem_from, sem_to)
that returns sub-dictionary of students that are on any semester starting fromsem_from
up tosem_to
inclusive. - Write function
how_many_use(students, os_name)
that returns how many students useos_name
operating system. - Write function
get_all_names(students)
that returns list of all the names of students.
With class...
- Create
Classroom
class that has contains list ofStudents
but has maximum number of students allowed (constructor parameter). - Each
Student
has unique id number, first and last name, and list of courses in current semester. - Add
add_student
,remove_student
,has_student
methods toClassroom
class where argument is object ofStudent
class. - Throw
AlreadyInClassroomException
exception if someone tries to add student of the same id as already in student list. - I know that I said list of
Students
but now I want to hold those objects in dictionary. (What should be used as key?)
class SomeClass(object):
def __init__(self, a, b = 5, c = []):
self.a = a
self.b = b
self.c = c # This is mad!