Basic Python Programming exercises
Python is a versatile and powerful programming language that is widely used in a variety of fields, from data science and machine learning to web development and automation. However, as with any programming language, it’s essential to practice in order to become proficient. In this article, we will discuss some basic Python exercises that are perfect for beginners looking to improve their skills.
- Printing to the console: One of the most basic exercises in Python is to practice printing text to the console. This can be done using the
print()
function, which takes a string as input and outputs it to the console. For example:
print("Hello, World!")
2. Variables and data types: Another important concept to practice is variables and data types. Python supports a variety of data types, including integers, floating-point numbers, strings, and booleans. To assign a value to a variable, use the assignment operator =
. For example:
x = 10
y = 20.5
name = "John Doe"
is_happy = True
3. Arithmetic operations: Python supports a wide range of arithmetic operations, including addition, subtraction, multiplication, division, and exponentiation. For example:
a = 10
b = 5
c = a + b
d = a - b
e = a * b
f = a / b
g = a ** b
4. Control flow: Control flow is an essential concept to practice in Python. This includes conditional statements such as if
, elif
, and else
, as well as loops such as for
and while
. For example:
x = 10
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")
for i in range(5):
print(i)
while x > 0:
print(x)
x -= 1
5. Functions and modules: Functions are a way to encapsulate a piece of code and reuse it multiple times. Python also supports the use of modules, which are collections of functions and variables that can be imported and used in other parts of a program. For example:
def add(x, y):
return x + y
def subtract(x, y):
return x - y
print(add(10, 5))
print(subtract(10, 5))
6. List, Tuples and Dictionaries: List, Tuples and Dictionaries are some data structures in python that can hold a collection of items. Lists are mutable, Tuples are immutable and Dictionaries are key-value pairs.
A. Lists:
- Create a list of the first 10 positive integers.
- Print the third element of the list.
- Append the number 15 to the list.
- Remove the last element of the list.
- Iterate over the list and print each element.
# Solution
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(my_list[2]) # Prints 3
my_list.append(15)
my_list.pop()
for num in my_list:
print(num)
B. Tuples:
- Create a tuple of the first five positive integers.
- Print the third element of the tuple.
- Try to change the third element to 10.
# Solution
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[2]) # Prints 3
# this will give error as tuples are immutable
my_tuple[2] = 10
C. Dictionaries:
- Create a dictionary that maps the string keys “apple”, “banana”, and “orange” to the values 4, 5, and 6 respectively.
- Print the value associated with the key “banana”.
- Add a key-value pair to the dictionary: “mango”: 7
- Remove the key-value pair with key “orange”
- Iterate over the key-value pairs in the dictionary and print them out.
# Solution
my_dict = {"apple": 4, "banana": 5, "orange": 6}
print(my_dict["banana"]) # Prints 5
my_dict["mango"] = 7
del my_dict["orange"]
for key, value in my_dict.items():
print(key, value)
Conclusion
These are basic programming exercises in Python. You can use any Development editor i.e PyCharm, Visual Studio Code, Jupyter Notebook etc for this purpose.
Thanks for reading.