# Chapter 1

• Using Python, print “During this course, I will learn to program”

• Create new variables where you compute following calculations

• 5 plus 5
• 3 times 3
• 7 minus 3
• 13 divided by 3.0 (obs: there is decimal number here)
• 3 divided by 2 (obs: this is not a decimal number)
• 10 divided by 5.0
• 11 divided by 4.0

Print each of the variables on separate lines

• Create a new variable and store five into it. On separate lines, increase that variable by three decrease it by four multiply it by five Your code must work even if one changes the first variable assignment

• Using Python, print:

``````*
***
*****
*******``````
• Using Python, print
``````1
2
3
4
5``````

## Chapter 2

• Using Python, print following numbers one per line. Use a maximum of one print sentence per exercise.
• 0-10
• 0-100
• 50-100

For example, 0-10 looks like this

``````    0
1
2
3
4
5
6
7
8
9
10``````
• For numbers 1 to 10, print per line the number and if it is even:
``````1 False
2 True
3 False
4 True``````
• Store a word into a variable. Print that word in

• upper case
• lower case
• Store a word into a variable. Print

• the length
• the third character
• the first character
• Print “Kekkonen” 199 times.
• Print following words, letter by letter, each letter on a separate line. Use maximum of one print sentence per bullet below.
• “cat”
• “a ball”
• “cat has a ball”

Print these also using the upper case.

• The Finnish social security number specifies the sex of its owner. It is based on the three characters after the birth date, i.e. 098. If it is even, the owner is female, otherwise male.

Write a program that has a variable in for the identifying number series and prints whether the user is a female or a male.

• Create a new variable that contains text, for example `word = "cats"` Check out what following operations create (i.e. store them into a new variable and print that variable)

• word + "!"
• "She said " + word
• "a" in word
• "b" in word
• Implement following rules using Python:
1. Variable has some text content, such as “cats”
2. If the text has letter s, print `There is a S :)` and otherwise `There is no S :’(`

Try also with “candy”, “multimedia” and “Sandra”.

• The following flowchart demonstrates an algorithm.

Implement it and try also numbers 5, 6, 7, 10

• Year is a leap year, if it can be divided by 4. However, if it is divided by 100, it is a leap year only if it can also be divided by 400.

• Is 1999 a leap year?
• Is 2000 a leap year?
• Is 1900 a leap year?
• Create a program, which checks if a letter is a vowel. It should look like this
``````a is a vowel
A is a vowel
c is NOT a vowel``````
• Define a new number variable and choose a value for it. If the variable + 1 is can be divided by three, increase the variable by two. Test by printing the final value of the variable and varying the initial value of that same variable.

• Compute the sum of 1 + 2 + …. + 100 using a loop structure
• Matti tries to calculate 1 2 3 11.

However the following program does not work, figure out why. Do not use the computer to solve this, but work with paper and pen. Type it down to the computer to ensure you’ve fixed it

``````fraction = 0
for current in range( 1 , 12 ):
fraction * current
fraction = current
print fraction``````
• Matti also tries to compute the sum from 1 to 1000, but it does not work. Figure out why and fix it. Do not use computer.

``````sum = 0
for i in range( 1, 1001 ):
sum = i
i = i + 1
print i``````
• How many of the numbers 1, …, 10000 can be divided by four?

Correct: 2500

• Calculate the sum all the numbers that can be divided by three between 1, …, 10000.

Correct: 16668333

• For numbers 1 to 500, print “Hurrey” for numbers that can not be divided with four. For other numbers, print the numbers.

# Chapter 3

• Print all the leap years between 1990 - 2050.|2016-10-26 08:20:43.192591|2016-10-26 08:23:52.540787|09|f|2|3 26|Count how many vowels there are in a sentence. Use your own example sentences. |2016-10-26 08:21:33.572187|2016-10-26 08:23:43.688177|10|f|2|3 27|For each of the numbers between 1 and 100, implement following rules if the number can be divided by three, print fizz if the number can be divided by five, print buzz if the number can be divided both by three and five, print fizzbuzz otherwise, print the number

So, it should look something like this

``````1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
````|2016-10-26 08:24:38.070981|2016-12-10 19:41:02.099972|07|f|2|3
28|Use comma to split sentence: “Coding is fun, but sometimes difficult.”|2016-10-26 08:27:36.019151|2016-10-26 08:27:36.019151|01|f|1|4
29|Transform a variable from textual form to
integer (e.g. “5” )
float (e.g. “3.1415”)
|2016-10-26 08:28:02.026987|2016-10-26 08:28:02.026987|02|f|1|4
30|In a variable there is a string with leading spaces (e.g. “     Why are there so much space? “. Figure out, how to remove them computationally.
|2016-10-26 08:28:43.537963|2016-10-26 08:28:43.537963|03|f|1|4
31|You have a full format Finnish social security number for someone born in 20th century. Using that, determine if the owner is male or female and how many years old she/he is. (so, the input is like “311299-056Z”|2016-10-26 08:29:50.252004|2016-10-26 08:29:50.252004|04|f|2|4
32|Variables a and b are boolean values (i.e. they have only two values, True or False). Test what different values of a and b produce with commands``````

a and b a or b not a not a and not b |2016-10-26 08:31:18.273473|2016-10-26 08:31:18.273473|05|f|1|4 33|Write a code that checks if a variable can be divided with 2 and the reminder is 1 when divided with 3. Use and and/or or commands. |2016-10-26 08:32:38.575534|2016-10-26 08:32:38.575534|06|f|2|4 34|Continue the previous exercise and check if any such numbers exists between range 1 - 1001.|2016-10-26 08:33:20.096817|2016-10-26 08:33:20.096817|07|f|2|4 35|Check if there is a vowel in a word and the third letter is ‘b’. Test with your own variables.|2016-10-26 08:33:55.756411|2016-10-26 08:33:55.756411|08|f|2|4 36|Print following words in reversed order (e.g. Matti -> ittaM). Do use looping structure, not e.g. some of the inbuilt system in Python. “cat” “ball” |2016-10-26 08:35:25.539625|2016-10-26 08:35:25.539625|01|f|2|5 37|Matti wrote a program that checks if a number is prime number. Prime numbers can be divided only by the number itself and 1. However, the program does not work. Figure why not and do fixes. Do not use a computer for this task.

``````
n = 11

for check in range( 2, n ):
prime = False
if n % check == 0:
prime = True
if prime:
print n, "is prime number"
else:
print n, "is not a prime number"|2016-10-26 08:35:48.497769|2016-10-26 08:35:48.497769|02|f|2|5
38|Using the fixed prime number checker (from the previous exercise), count how many prime numbers exists range of 2 and 1000.
|2016-10-26 08:36:40.637281|2016-10-26 08:36:40.637281|03|f|2|5
39|Count how many times there is an a-letter on these examples. Do use looping techniques, not e.g. the count function
‘kissa’
‘sahatavarasatama’
|2016-10-26 08:37:19.810971|2016-10-26 08:37:19.810971|04|f|2|5
40|Create a function which prints “This function works”|2016-10-26 08:38:49.600730|2016-10-26 08:38:49.600730|01|f|3|6
41|Create a function which prints “Kekkonen” 199 times|2016-10-26 08:39:07.892351|2016-10-26 08:39:07.892351|02|f|3|6
42|Create a function, which calculates the sum between 0 and 50.|2016-10-26 08:39:41.932677|2016-10-26 08:39:41.932677|03|f|3|6
43|Create a function which calculates the sum between 50 and 100.|2016-10-26 08:39:58.398520|2016-10-26 08:39:58.398520|04|f|3|6
44|Following is an example of function code. What does it print? (Do not run the code)``````

def cats( text ): text = text + “ has cats!”

text = “dogs” cats( text ) print text |2016-10-26 08:40:56.492393|2016-10-26 08:40:56.492393|05|f|3|6 45|Following is an example of function code. What does it print? (Do not run the code)

``````def cats( text ):
text = text + “ has cats!”
return text

text = “dogs”
text = cats( text )
print text|2016-10-26 08:42:24.033539|2016-11-25 10:20:19.054213|06|f|3|6
46|Following is an example of function code. What does it print? (Do not run the code)
``````

def cats( text ): text = text + “ has cats!” return text

text = “dogs” cats( text ) print text |2016-10-26 08:43:09.576836|2016-11-25 10:22:13.111507|07|f|3|6 47|Following is an example of function code. What does it print? (Do not run the code)

``````def cats( text ):
text = text + “ has cats!”
return text

text = “dogs”
print cats( text )
print text
|2016-10-26 08:43:54.652406|2016-11-25 10:22:30.343356|08|f|3|6
48|Create a function which takes a text parameter and prints it|2016-10-26 09:43:12.643919|2016-10-26 09:43:12.643919|01|f|3|7
49| Create a function which takes a text parameter and returns it|2016-10-26 09:43:40.242646|2016-10-26 09:43:40.242646|02|f|3|7
50|Create a function which returns “This function works”|2016-10-26 09:44:42.608487|2016-10-26 09:44:42.608487|03|f|3|7
51|Create a function which calculates the sum between two given parameters|2016-10-26 09:45:10.782342|2016-10-26 09:45:10.782342|04|f|3|7
52|Define a function, which takes the number of student grant months as input and calculates based on that the maximum income for the student in that year.

Also create a function which takes the monthly income and returns the highest number of student grant months.

See [Kela page](http://www.kela.fi/web/en/student-s-own-income) for details.
|2016-10-26 09:46:45.677129|2016-12-09 21:38:09.081776|05|f|5|7
54|Create a function, which takes text as parameter and counts the number of letters, number of words and number of question marks in the text and prints them in separate lines.|2016-10-26 09:47:49.219663|2016-10-26 09:47:49.219663|06|f|3|7
55|Create a function which takes text as parameter and returns it reversed (e.g. code -> edoc). Use a lopping structure.|2016-10-26 09:48:18.957306|2016-10-26 09:48:18.957306|07|f|2|7
56|Create a function, which takes three number parameters and returns the largests of them. Do not use pre-made max (or other) functions for this.|2016-10-26 09:48:46.034388|2016-10-26 09:48:46.034388|08|f|3|7
57|Create a function, which returns True if a number given as parameter is prime number and False otherwise.|2016-10-26 09:49:03.982576|2016-10-26 09:49:03.982576|09|f|3|7
58|Using the function on the previous exercise, calculate how many prime numbers are between 2 and 10000000.|2016-10-26 09:49:52.988029|2016-10-26 09:49:52.988029|10|f|3|7
59|Define a function, which takes the monthly rent paid by the student and returns the housing supplement based on that income. See Kela page http://www.kela.fi/web/en/housing-supplement_amount for details.|2016-10-26 09:50:38.543062|2016-10-26 09:53:06.876962|11|f|5|7
61|Create a new variable with a type of list, and put numbers 1, 2, 3 and 4 into it. Print the list.
|2016-10-26 09:54:36.821989|2016-10-26 09:54:36.821989|01|f|4|8
62|1. Now add number 5 into the same list and print it
2. Print the third item on the list
3. Print the number of items (length) of the list
|2016-10-26 09:55:24.429932|2016-10-26 09:55:24.429932|02|f|4|8
63|Create a function, which takes a list as parameter and prints all elements in that list.|2016-10-26 09:55:57.297732|2016-10-26 09:55:57.297732|03|f|4|8
64|Create a function, which takes a list as parameter and returns the largest element on that list. Do not use the max function defined in Python.|2016-10-26 09:56:14.017242|2016-10-26 09:56:14.017242|04|f|4|8
65|You have a list of numbers and you need to calculate the sum and mean of the numbers on that list. Ignore all numbers below zero in this calculation. Use a for-loop.
|2016-10-26 09:56:53.985434|2016-10-26 09:56:53.985434|05|f|2|8
66|You have candidate names in a dictionary

* print the party of candidate Rinne
* print all candidate names in separate lines
* print all candidate parties in separate lines
``````

candidates = { 'Stubb' : 'KOK', 'Rinne' : 'SDP', 'Soini' : 'PS', 'Haavisto' : 'Vihreat', 'Arhimäki' : 'VAS', 'Andersson' : 'VAS', 'Pekkarinen' : 'KESK', 'Sipila' : 'KESK'

}

|2016-10-26 09:57:37.722547|2016-11-12 21:16:31.514247|06|f|4|8 67|Print the number of candidates in different parties. Hint: use dictionary to collect the frequencies.|2016-10-26 09:58:13.380124|2016-10-26 09:58:13.380124|07|f|4|8 68|Create a list and add numbers 0 to 1000 into it. |2016-10-26 09:58:34.975793|2016-10-26 09:58:34.975793|08|f|4|8 69|Create a list of some numbers (choose them), and check if numbers 2, 5 or 7 are in that list. |2016-10-26 09:58:55.804894|2016-10-26 09:58:55.804894|09|f|4|8 70|Create a list of some numbers (choose them), and create a function to check if other list of numbers (such as 2, 5 and 7) are in that list. It should return the number of same numbers in both of the lists. Do not use set operations. |2016-10-26 10:00:13.048636|2016-11-25 10:45:57.402740|10|f|4|8 71|Using following functions, write the code for week 2 case study.

``````    femaleNames() gives a list of female names [‘Anna’, ‘Anne’, ...]
similarly, there is  maleNames()
stories() returns a list that has story texts and liberal / conservative position in a dictonary [ { ‘text’ : ‘Loreum ipsulam’ , ‘position’ : -1  } , { ‘text’ : ‘and so on’ , ‘position’ : 1  } , ... ]

|2016-10-26 10:02:58.204971|2016-11-25 10:24:47.504195|01|f|5|9
72|Re-implement the case study from week 3. Define the methods you need for your work (see previous exercise)|2016-10-26 10:03:40.706107|2016-10-26 10:03:40.706107|02|f|5|9
73|Create a function that returns the minimum number from a list (given as parameter). Do not use min (or other) pre-made functions.|2016-10-26 10:04:14.293085|2016-10-26 10:04:14.293085|03|f|2|9
74|Using Exercises (7/11) and (7/5).

* Calculate the monthly support for a student with rents of 150, 175, 200, 225, 250, 275 and 300.|2016-10-26 10:06:47.256777|2016-11-25 10:32:40.733106|04|f|2|9
75|Using Exercises (7/11) and (7/5)

* Calculate the yearly support for student with rents of 150, 175, 200, 225, 250, 275 and 300 and monthly income of 500, 750, 1000, 1250, 1500 euros.|2016-10-26 10:07:42.946663|2016-12-09 21:35:19.327203|05|f|2|9
76|Using a Monte Carlo simulation (which was introduced in microsimulation case article), try to compute the value of pi. The process is as follows:

repeat many times:
get two random numbers between the range 0, 1.
if the random numbers’ combined distance from 0 is less than 1, increase the positive cases. You need to use Pythagoras's theorem here to compute the distance.

In the end, check the ratio of positive cases to the number of tries, and multiply that by 4.

You’ll get random numbers with``````

import random ## needs to be done only once r = random.random() |2016-10-26 10:08:02.532767|2016-11-25 10:44:11.334415|06|f|5|9 77|File jytky_verkko.txt includes links on who tweeted to whom:

``````1620879505 -> 125463318
527224088 -> -
584610998 -> -
138152865 -> 283159564``````

User 1620879505 has mentioned user 125463318, whereas user 527224088 has not mentioned anyone

• How many tweets are not directed to anyone?
• Who is the most active tweeter (sends out most tweets) Use dictionary here; do not use pre-made functions such as counter.|2016-10-26 10:11:15.711001|2016-12-10 15:16:59.738680|05|f|4|10 78|File jytky_verkko.txt includes links on who tweeted to whom:
``````1620879505 -> 125463318
527224088 -> -
584610998 -> -
138152865 -> 283159564``````

User 1620879505 has mentioned user 125463318, whereas user 527224088 has not mentioned anyone.

• Who sends out most tweets which do not mention others? Use dictionary here; do not use pre-made functions such as counter. |2016-10-26 10:11:55.042429|2016-12-10 15:18:46.362195|07|f|4|11 79|There is a text file “jytky.txt” which contains some tweets from Finnish elections. Each tweet is on its own line. How many tweets there are in total in this file? Remove lines that are empty.

``````
print line
|2016-10-26 10:13:01.252802|2016-12-10 15:15:26.279190|01|f|5|10
80|The Finnish tax office has released corporate tax information as [open data](https://www.vero.fi/fi-FI/Avoin_data).

For example, in 2012 file this information is stored in format year;register ID;Corporation name;Location;Taxable income;Paid taxes;Pre taxes;Tax return;Taxes to be paid

* Which corporation had highest taxable income?
* Which corporation received highest tax returns?
|2016-10-26 10:13:33.523012|2016-12-10 15:25:28.086805|02|f|5|10
81|Search from the “jytky.txt” file all Tweets which include the word “RT” (i.e. it seems they are retweeted) and write those into file “jytky_myname.txt”
|2016-10-26 10:14:00.133377|2016-10-26 10:14:00.133377|03|f|5|10
82|Using BeautifulSoup, figure which of the discussions in Vauva.fi forums received most responses. E.g. http://www.vauva.fi/keskustelu/alue/perhe_ja_arki|2016-10-26 10:14:35.311763|2016-12-16 08:22:09.910945|04|f|5|10
83|Based on the tax office information, calculate which city had highest `paid taxes`.|2016-10-26 10:16:36.720633|2016-12-10 15:31:09.694147|01|f|5|11
84|Create a function tree( height ) which takes a height as parameter, and prints a tree of that height. I.e.
``````

*

```|2016-10-26 10:17:15.172848|2016-12-23 08:24:29.615329|02|f|3|11 85|Create an empty list, and for numbers between 1 to 10, create a list which contains that number of a-letters in a list, e.g.

[ [ ‘a’ ] , [ ‘a’ , ‘a’ ] , [ ‘a’ , ‘a’, ‘a’ ] …. ] |2016-10-26 10:17:52.351640|2016-10-26 10:17:52.351640|03|f|4|11 86|From the file “jytky.txt” collect all tweet (texts) mentioning a user to a dictionary. The dictionary key is the username and the dictionary value is the list of the tweet texts; i.e.

``````

{ “user1” : [ “Tweet 1 text @user1”, “Tweet 2 text @user1 @user2” ], “user2” : [“Tweet 2 @user1 @user2”, “Tweet 3 @user2” ] }
|2016-10-26 10:18:19.397190|2016-10-26 10:18:19.397190|04|f|4|11
87|Based on dictionary developed on the previous exercise, compute how many times each user is mentioned in a tweet which contains a reference to a) Helsingin Sanomat / HS, b) YLE, c) Iltasanomat or d) Iltalehti (names of Finnish newspapers). Print each username and the number of tweets next to it.
|2016-10-26 10:19:12.748642|2016-10-26 10:19:12.748642|05|f|4|11
88|Create a function tree( height ) which takes a height as parameter, and prints a tree (see V5) of that height.|2016-10-26 10:19:49.485603|2016-10-26 10:19:49.485603|06|f|3|11
89|Do Artist 1 exercise stage from [code.org](https://studio.code.org/s/20-hour/).|2016-11-01 06:47:18.379682|2016-12-10 16:04:24.498052|Artist 1 in code.org|f|6|12
91|Do Artist 2 exercise stage from [code.org](https://studio.code.org/s/20-hour/).|2016-11-01 06:48:28.976990|2016-12-10 16:04:34.507274|Artist 2 in code.org|f|6|12
92|Do Artist 3 exercise stage from [code.org](https://studio.code.org/s/20-hour/).|2016-11-01 06:48:57.569826|2016-12-10 16:04:45.308593|Artist 3 in code.org|f|6|12
93|Participate in the research study related to topic models (a computational method used rather a lot in computational social sciences to mine texts). The experiment will take about one hour. Participate by mailing Matti and choose a time and date.

The data analyzed is in Finnish, thus Finnish skills are required for this study.|2016-11-23 16:42:42.000636|2016-12-10 16:05:03.358444|Participation in the topic model study [Finnish language skills required]|f|6|12
94|Those who do not know Finnish can take part in about one-hour pilot study about Facebook data analysis.

We ask you to reflect your Facebook behavior in an interview setting. Contact Matti to arrange this time.|2016-11-23 16:45:13.790959|2016-12-10 16:04:54.514187|Participation in the Facebook data study [for non-Finnish speaking students]|f|6|12``````