Python Exercises
python exercises for beginner programmers. If you are looking for a python challenge and are a beginner programmer, this might be for you. These exercises will help you with Python training.
Python for dummies? No, challenging exercises to become a good developer!
You can download the answers here.
Related course: Complete Python Programming Course & Exercises
Python Practice
Beginner exercises
- Make a Python program that prints your name.
- Make a program that displays the lyrics of a song.
- Make a program that displays several numbers.
- Make a program that solves and shows the summation of 64 + 32.
- Do the same as in 2, but make it sum x + y.
- Make a program that displays your favourite actor/actress.
- Try to print the word ‘lucky’ inside s.
- Try to print the day, month, year in the form “Today is 2/2/2016”.
- Try the replace program
- Can a string be replaced twice?
- Does replace only work with words or also phrases?
- Find out if string find is case sensitive
- What if a query string appers twice in the string?
- Write a program that asks console input and searches for a query.
- Create a list of words and join them, like the example above.
- Try changing the seperator string from a space to an underscore.
- Can a string be split on multiple characters?
- Can you split a string this string?: World,Earth,America,Canada
- Given an article, can you split it based on phrases?
- Make a program that creates a random number and stores it into x.
- Make a program that prints 3 random numbers.
- Create a program that generates 100 random numbers and find the frequency of each number.
- Make a program that asks a phone number.
- Make a program that asks the users preferred programming language.
- Make a program that asks the number between 1 and 10. If the number is out of range the program should display “invalid number”.
- Make a program that asks a password.
- Make a program that lists the countries in the set
1
clist = ['Canada','USA','Mexico','Australia']
- Create a loop that counts from 0 to 100
- Make a multiplication table using a loop
- Output the numbers 1 to 10 backwards using a loop
- Create a loop that counts all even numbers to 10
- Create a loop that sums the numbers from 100 to 200
Make a program that lists the countries in the set below using a while loop.
1
clist = ["Canada","USA","Mexico"]
What’s the difference between a while loop and a for loop?
- Can you sum numbers in a while loop?
- Can a for loop be used inside a while loop?
- Make a function that sums the list mylist = [1,2,3,4,5]
- Can functions be called inside a function?
- Can a function call itself? (hint: recursion)
- Can variables defined in a function be used in another function? (hint: scope)
- Make a program that displays the states in the U.S.
1
states = [ 'Alabama', .. ,'Wyoming' ]
- Display all states starting with the letter M
- Given the list y = [6,4,2] add the items 12, 8 and 4.
- Change the 2nd item of the list to 3.
- Given a list with pairs, sort on the first element
x = [ (3,6),(4,7),(5,9),(8,4),(3,1)] - Now sort on the second element
- Create a list of one thousand numbers
- Get the largest and smallest number from that list
- Create two lists, an even and odd one.
- Make a mapping from countries to country short codes
- Print each item (key and value)
- Read a file and number every line
- Find out what the program does if the file doesn’t exist.
- What happens if you create a file with another user and try to open it?
- Write the text “Take it easy” to a file
- Write the line open(“text.txt”) to a file
- Given a tic-tac-toe board of 3x3, print every position
- Create a program where every person meets the other
persons = [ “John”, “Marissa”, “Pete”, “Dayton” ] - If a normal for loop finishes in n steps O(n), how many steps has a nested loop?
Take a slice of the list below:
1
pizzas = [“Hawai”,”Pepperoni”,”Fromaggi”,”Napolitana”,”Diavoli”]
Given the text “Hello World”, take the slice “World”
- Create a function that returns a,b and a+b
- Create a function that returns 5 variables
- Add a function reduce amount that changes the variable balance
- Create a function with a local variable
- Print the date in format year-month-day
- Can try-except be used to catch invalid keyboard input?
- Can try-except catch the error if a file can’t be opened?
- When would you not use try-except?
OOP exercises
- Can you have more than one class in a file?
- Can multiple objects be created from the same class?
- Can objects create classes?
- Using the code above, create another object
- Add a method to the class: location()
- Add a variable age and create a getter and setter
- Why would you use getter and setter methods?
- Import the math module and call the sin function
- Create your own module with the function snake()
- Create a new class that inherits from the class App
- Try to create a class that inherits from two super classes (multiple inheritance)
- Can a method inside a class be called without creating an object?
- Why does not everybody like static methods?
- What is an iterable?
- Which types of data can be used with an iterable?
- What is a classmethod?
- How does a classmethod differ from a staticmethod?
- Do all programming languages support multiple inheritance?
- Why would you not use multiple inheritance?
- Is there a limit to the number of classes you can inherit from?