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

Run Python programs

  • Make a Python program that prints your name.
  • Make a program that displays the lyrics of a song.

Variables

  • 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.

Strings

  • 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”.

String replace

  • Try the replace program
  • Can a string be replaced twice?
  • Does replace only work with words or also phrases?

String find

  • 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.

String join

  • Create a list of words and join them, like the example above.
  • Try changing the seperator string from a space to an underscore.

String split

  • 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?

Random numbers

  • 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.

Keyboard input

  • Make a program that asks a phone number.
  • Make a program that asks the users preferred programming language.

If statements

  • 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.

For loops

  1. Make a program that lists the countries in the set
    1
    clist = ['Canada','USA','Mexico','Australia']
  2. Create a loop that counts from 0 to 100
  3. Make a multiplication table using a loop
  4. Output the numbers 1 to 10 backwards using a loop
  5. Create a loop that counts all even numbers to 10
  6. Create a loop that sums the numbers from 100 to 200

While loops

  1. Make a program that lists the countries in the set below using a while loop.

    1
    clist = ["Canada","USA","Mexico"]
  2. What’s the difference between a while loop and a for loop?

  3. Can you sum numbers in a while loop?
  4. Can a for loop be used inside a while loop?

Functions

  1. Make a function that sums the list mylist = [1,2,3,4,5]
  2. Can functions be called inside a function?
  3. Can a function call itself? (hint: recursion)
  4. Can variables defined in a function be used in another function? (hint: scope)

Lists

  • Make a program that displays the states in the U.S.
    1
    states = [ 'Alabama', .. ,'Wyoming' ] 
  • Display all states starting with the letter M

List operations

  • Given the list y = [6,4,2] add the items 12, 8 and 4.
  • Change the 2nd item of the list to 3.

Sorting list

  • 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

Range function

  • Create a list of one thousand numbers
  • Get the largest and smallest number from that list
  • Create two lists, an even and odd one.

Dictionary

  • Make a mapping from countries to country short codes
  • Print each item (key and value)

Read file

  • 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 file

  • Write the text “Take it easy” to a file
  • Write the line open(“text.txt”) to a file

Nested loops

  • 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?

Slices

  • Take a slice of the list below:

    1
    pizzas = [“Hawai”,”Pepperoni”,”Fromaggi”,”Napolitana”,”Diavoli”]
  • Given the text “Hello World”, take the slice “World”

Multiple return

  • Create a function that returns a,b and a+b
  • Create a function that returns 5 variables

Scope

  • Add a function reduce amount that changes the variable balance
  • Create a function with a local variable

Time and date

  • Print the date in format year-month-day

Try except

  • 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

Class

  • 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()

Getter and setter

  • Add a variable age and create a getter and setter
  • Why would you use getter and setter methods?

Modules

  • Import the math module and call the sin function
  • Create your own module with the function snake()

Inheritance

  • Create a new class that inherits from the class App
  • Try to create a class that inherits from two super classes (multiple inheritance)

Static method

  • Can a method inside a class be called without creating an object?
  • Why does not everybody like static methods?

Iterable

  • What is an iterable?
  • Which types of data can be used with an iterable?

Classmethod

  • What is a classmethod?
  • How does a classmethod differ from a staticmethod?

Multiple inheritance

  • 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?