Table of Contents
- Introduction to Python Exercises for Beginners
- Write a simple program that accomplises the following tasks.
Introduction to Python Exercises for Beginners
Learning programming with only reading is not the way for programmers. We create and code and practice new concepts and make new programs with the learnt concepts. Therefore, we have created a list of Python exercises for Beginners which will give you assistance in your practice.
You can also visit our previous post for more exercises.
Write a simple program that accomplises the following tasks.
1 Store a message in a variable and print that message.
Solution
message = ‘I am a good programmer’
print(message)
>>I am a good programmer
2 Store a message in a variable, and print that message. Then change the value of your variable to a new message, print a new message.
Solution
message = ‘I am a good programmer’
print(message)
>>I am a good programmer
3 Store a person’s name in a variable, and print a message to that person. Your message can be anything like, ‘Hello James!’
Solution
name = 'Sandy'
print("Hello " + name + ", how are you today?")
>>Hello Sandy, how are you today?
4 Store a person’s name in a variable, and then print the person’s name in lowercase, uppercase and titlecase.
Solution
name = 'Calsie'
print(name.upper())
print(name.lower())
print(name.title())
>>CALSIE
>>calsie
>>Calsie
5 Find a famous quote from a person you admire. Print the quote and name of the person. Your output can look something like this,
‘Tim Berners Lee once said “We can’t blame the technology when we make mistakes.” ‘
Solution
famous_quote = 'Albert Einstein once said, "We cannot solve our problems with the same thinking we used when we created them." '
print(famous_quote)
>>We cannot solve our problems with the same thinking we used when we created them
6 Repeat the exercise (5) but this time, store the famous person’s name in a variable and the quote onto another variable, and finally print the message.
Solution
famous_quote = "If you can't explain it simply, you don't understand it well enough."
famous_person = 'Albert Einstein'
print(famous_person + ' once said, ' + ' " ' + famous_quote + "." + ' " ')
>>Albert Einstein once said, "If you can't explain it simply, you don't understand it well enough."
7 Store a message including whitespaces and then use rstrip(), lstrip() and strip() functions on it and print it.
Solution
message_1 = 'Hello, how is your practice? '
print(message_1.rstrip())
message_2 = ' Programming is fun, isn't it?'
print(message_2.lstrip())
message_3 = ' Walk through exercises like it's wind ' print(message_3.strip())
>>Hello, how is your practice?
>>Programming is fun, isn't it?
>>Walk through exercises like it's wind
8 Write addition, subtraction, multiplication and division operation that each result in the number 16.
Solution
print(8+8)
print(22-6)
print(4*4)
print(180/10)
>>16
>>16
>>16
>>16
9 Store your favourite number in a variable and then print that message to reveal that number.
Solution
fav_num = '10'
print("It's a secret, my favourite number is " + fav_num + ".")
>>It's a secret, my favourite number is 10.
10 Place the parenthesis correctly to get the desired answer.
a = 5
b = 9
c = 12
a + c – b = 8
Solution
a = 5
b = 9
c = 12
print(a + (c - b))
>> 8
Leave a Reply