Your First Python Coding Drills:
Exercise 1: Hello, World!
print("Hello, World!")
Setup Steps:
1. Install Python: Go to the official Python website (https://www.python.org) and download the latest version of Python for your operating system. Follow the installation instructions provided.
2. Open a text editor: Open VSC to write your Python code.
3. Write the code: In your text editor, type the code exactly as shown above.
4. Save the file: Save the file with a .py extension, for example, `hello.py`.
5. Run the program: Open a terminal or command prompt and navigate to the directory where you saved the file. Then, enter the following command to run the program:
```
python hello.py
```
You should see the output `Hello, World!` displayed on the screen.
Exercise 2: Even or Odd?
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")
```
Setup Steps:
1. Open a text editor and create a new file.
2. Write the code: Copy the code above into your text editor.
3. Save the file: Save the file with a .py extension, for example, `even_odd.py`.
4. Run the program: Open a terminal or command prompt and navigate to the directory where you saved the file. Then, enter the following command to run the program:
```
python even_odd.py
```
The program will ask you to enter a number. After entering the number, it will determine whether it is even or odd and display the result.
Exercise 3: Calculator
```python
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operation = input("Enter the operation (+, -, *, /): ")
result = None
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
result = num1 / num2
else:
print("Invalid operation")
if result is not None:
print("Result:", result)
```
Setup Steps:
1. Open a text editor and create a new file.
2. Write the code: Copy the code above into your text editor.
3. Save the file: Save the file with a .py extension, for example, `calculator.py`.
4. Run the program: Open a terminal or command prompt and navigate to the directory where you saved the file. Then, enter the following command to run the program:
```
python calculator.py
```
The program will prompt you to enter two numbers and an operation (+, -, *, /). After entering the inputs, it will perform the operation and display the result.
Exercise 4: Fibonacci Sequence
Note: def is how we create a function - a reusable block of Code: In Python
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
seq = [0, 1]
while len(seq) < n:
next_num = seq[-1] + seq[-2]
seq.append(next_num)
return seq
num = int(input("Enter a number: "))
fib_seq = fibonacci(num)
print("Fibonacci Sequence:", fib_seq)
```
Setup Steps:
1. Open a text editor and create a new file.
2. Write the code: Copy the code above into your text editor.
3. Save the file: Save the file with a .py extension, for example, `fibonacci.py`.
4. Run the program: Open a terminal or command prompt and navigate to the directory where you saved the file. Then, enter the following command to run the program:
```
python fibonacci.py
```
The program will prompt you to enter a number. After entering the number, it will generate the Fibonacci sequence up to that number and display the result.
Exercise 5: File Manipulation
```python
filename = input("Enter the filename: ")
sentence = input("Enter a sentence: ")
with open(filename, "a") as file:
file.write(sentence + "\n")
with open(filename, "r") as file:
content = file.read()
print("File Contents:")
print(content)
```
Setup Steps:
1. Open a text editor and create a new file.
2. Write the code: Copy the code above into your text editor.
3. Save the file: Save the file with a .py extension, for example, `file_manipulation.py`.
4. Run the program: Open a terminal or command prompt and navigate to the directory where you saved the file. Then, enter the following command to run the program:
```
python file_manipulation.py
```
The program will ask you to enter a filename and a sentence. It will then append the sentence to the file and display the contents of the file.
Exercise 6: System Monitoring Script
```python
import psutil
import time
while True:
cpu_percent = psutil.cpu_percent()
memory_percent = psutil.virtual_memory().percent
disk_percent = psutil.disk_usage("/").percent
print("CPU Usage:", cpu_percent, "%")
print("Memory Usage:", memory_percent, "%")
print("Disk Usage:", disk_percent, "%")
time.sleep(5)
```
Setup Steps:
1. Open a text editor and create a new file.
2. Write the code: Copy the code above into your text editor.
3. Save the file: Save the file with a .py extension, for example, `system_monitor.py`.
4. Run the program: Open a terminal or command prompt and navigate to the directory where you saved the file. Then, enter the following command to run the program:
```
python system_monitor.py
```
The program will continuously monitor the system resources (CPU usage, memory usage, disk usage) and display the current usage statistics every 5 seconds.
These exercises are designed for new learners with no programming experience. Follow the setup steps provided for each exercise to create and run the Python programs. These exercises will introduce fundamental concepts and prepare learners for the next session on Linux network programming and operating system administration.