Learn+Python+by+solving+problems
Learn+Python+by+solving+problems
1. Introduction
Exercise 1 – Hello.py
# Write a program to enter your name (first name,
surname), the program should repond hello to you.
2. Decision making
Exercise 13 – Biggest number.py
# Determine the biggest number between 2 numbers and
dispaly it an appropiate message.
Exercise 15 – Is it between.py
# Enter a number. If the number is between -10 and 10 then
tell if it is positive, negative or 0. If number is outside range
show an error message.
Exercise 18 – Triangles.py
# Write a program that reads the lengths of a triangle sides
and determine if it is equilateral (all sides the same), isoscele (2
sides same length) or scalene (all sides different).
Exercise 19 – Is it a hoiday.py
# Write a program that reads a day and a month. If the date
matches : Christmas, New Year or Halloween, print the holiday.
Exercise 22 – Overcharged.py
# A phone plan includes 100 minutes and messages and 6 GB
of data for £10 a month. Every minute over limit is charged at
0.20p, every text at 0.08p and every 1 GB or less at £5.
# There is also a charge of 0.36p per month to support
emergency call centers. The entire bill is subject to 6 percent
tax.
# Take input number of minutes, texts and GB used and
calculate the total of the bill with each charge printed on it`s
own line.
3. Loops
Exercise 25 – Name name name.py
# Enter your name and display it 5 times. Upgrade the program
to display it for a number of times chosen by you.
Exercise 26 – Na m e n a m e n a m e.py
# Enter your name and a number. Display your name one
letter at the time on same line for that number of times.
Exercise 27 – Total.py
# Create a program where you add 5 numbers to the total.
After each number print the total.
# Extend the program by asking if the numbers should be
added.
Exercise 31 – Palindrome.py
# A palindrom is a word that reads the same backwards as
forwards. ex: eye, madam, level, radar. Check if a string is a
palindrom.
Exercise 35 – Test.py
# Create a small test using 2 different random numbers every
question. Use only addition. 10 questions with one point for
each, print score at the end.
Exercise 39 – 10 elephants.py
# Using the song :
# 1 elephant swings happily
# On a spider web this morning
# The web is strong, it doesn't break
# So he calls another elephant
# Write a program that will ask you for the number of
elephants. If number is right, print next lyrics up to 5.
# If number is wrong print a error and ask user to try again.
4. Functions
Exercise 62 – Is it prime.py
# Create a function that can determine if number is prime.
Exercise 64 – 3 numbers.py
# Take 3 random numbers as parameters and determine the
average value.
Exercise 65 – Online shipping.py
# An online retailer offers shipping at the cost of 3.10 for the first
item and 0.95 for each item after.
# Create a function that takes as input the number of items
ordered and prints the shipping cost.
Exercise 68 – Perfect.py
# Create a function that will determine if a number is perfect.
# A perfect number is a positive integer that is equal to the sum
of its positive divisors excluding the number itselt.
# Check how many perfect numbers are in 1 million.
Exercise 71 – Calculator.py
# Create a simple calculator function that will be able to calculate
+ and – operations. It should work like every calculator,
# after first 2 numbers are calculated, you can keep making
operations to the result.
# Break this problem in 3 separate functions: addition,
substraction, main.
Exercise 72 – FizzBuzz.py
# Fizz buzz is a group word game for children to teach them
about division.
# Players take turns to count, replacing any number divisible by
three with the word "fizz",
# any number divisible by five with the word "buzz" and any
number divisible by three and five with "fizzbuzz".
# Crate a program that will do this for numbers to 100.
# 1 2 3 4 5 6 7 8 9 10
# 1 1 2 3 4 5 6 7 8 9 10
# 2 2 4 6 8 10 12 14 16 18 20
# 3 3 6 9 12 15 18 21 24 27 30
# 4 4 8 12 16 20 24 28 32 36 40
# 5 5 10 15 20 25 30 35 40 45 50
# 6 6 12 18 24 30 36 42 48 54 60
# 7 7 14 21 28 35 42 49 56 63 70
# 8 8 16 24 32 40 48 56 64 72 80
# 9 9 18 27 36 45 54 63 72 81 90
# 10 10 20 30 40 50 60 70 80 90 100
Exercise 75 – Max.py
# Create 100 random numbers between 1,1000. Select the first
number as the maximum and generate
# the other 99. Update the maximum every time you find a larger
number and keep track how of updates.
Exercise 77 – Cipher.py
# The Caesar Cipher was one of the earliest ciphers ever
invented.
# In this cipher, you encrypt a message by taking each letter in
the message
# and replacing it with a “shifted” letter.
# If you shift the letter A by one space, you get the letter B.
# If you shift the letter A by two spaces, you get the letter C and
so on.
# Create a program that will allow you to encrypt or decrypt a
string.
# You will give it a key(shift amont) and a string(message). Make
sure it works for uppercase letters,
# keys over 26, and also you are able to decrypt previous
messages.
6. Lists
Exercise 83 – No duplicates.py
# Remove duplicates from the list = ['apple', 'banana', 'apple',
'orange', 'plum', 'plum']
Exercise 84 – Alphabet.py
# Create 2 lists: lowercase and uppercase alphabet. Use the ASCII
table to create them.
Exercise 85 – Lottery.py
# Create a list with 6 numbers chosen by you.
# Simulate a lottery draw, from 1 to 49 draw 6 numbers(no
duplicates).
# Print both lists and compare them. Print how much you won.
# 0,1 numbers - Nothing
# 2 numbers - 10
# 3 numbers - 30
# 4 numbers - 4500
# 5 numbers - 100000
# 6 numbers – 1000000
Exercise 90 – Manipulation.py
# Create a list with 20 random elements from -10 to 10.
# Break it into 3 lists, negatives, positives and zeros.
# For the negative and positive numbers list ask for 1 number in
each and completly remove it.
7. Dictionaries
Exercise 94 – Power.py
# Create a dictinary with the numbers from 1-20 as keys and
number to power 2 as value.
Exercise 95 – Manipulation.py
# Create a dictionary with 10 random numbers between 1-1000.
# Calculate the average of values, if a value is under the average
add it to a sum.
Exercise 98 – Discounts.py
# A shop is running a discount for some products. Determine
how much the product is discounted in %.
# Ex: Bread: curent price: 0.5, normal price: 1 -- > Discount
amount is 50%
Exercise 99 – Anagrams.py
# Create a program that will take two strings as input and
determine if they are anagrams.
# An anagram is a word or phrase formed by rearranging the
letters of a different word or phrase.
# Ex: "evil" = "vile", "eleven plus two" = "twelve plus one"