Sample Exercises - Check back soon for more!
Lux Labz Training Exercises
Filename | Action |
---|---|
exercise_001.md | Preview |
exercise_002.md | Preview |
exercise_02.md | Preview |
Preview: exercise_001.md
Getting Started with Python: A Beginner's Exercise
Welcome to your first Python programming exercise! This guide will walk you through the basics of Python, covering fundamental concepts and giving you hands-on experience. By the end of this exercise, you'll have a solid understanding of Python basics and will have written your first Python program.
Prerequisites
Before you begin, ensure you have the following:
- Python Installed: Download and install Python from the official website python.org. Python 3.x is recommended.
- Code Editor: Choose a code editor to write your Python code. Options include:
- Visual Studio Code: Download here
- PyCharm: Download here
- Sublime Text: Download here
- Basic Computer Skills: Familiarity with basic computer operations and navigation.
Steps
1. Set Up Your Environment
Install Python:
- Download Python from python.org.
- Follow the installation instructions for your operating system (Windows, macOS, Linux).
- Ensure the option to add Python to your PATH is checked during installation.
Verify Python Installation:
- Open your command line or terminal.
- Type
python --version
and press Enter. You should see the installed version of Python. - Optionally, you can use
python3 --version
if your system differentiates between Python 2 and 3.
2. Write Your First Python Script
Open Your Code Editor:
- Launch the code editor you installed.
Create a New File:
- In your editor, create a new file and name it
hello.py
.
- In your editor, create a new file and name it
Write Code:
- In
hello.py
, type the following code:# This is a simple Python program that prints "Hello, World!" print("Hello, World!")
- In
Save Your File:
- Save the file with the
.py
extension.
- Save the file with the
3. Run Your Python Script
Open Command Line or Terminal:
- Navigate to the directory where you saved
hello.py
using thecd
command.
- Navigate to the directory where you saved
Execute Your Script:
- Type
python hello.py
and press Enter. If your system usespython3
, typepython3 hello.py
instead. - You should see
Hello, World!
printed to the screen.
- Type
4. Explore Basic Python Concepts
Variables:
- Variables are used to store data. For example:
name = "Alice" age = 30 print(name) print(age)
- Variables are used to store data. For example:
Data Types:
- Python supports various data types, including integers, floats, strings, and lists. Try experimenting with these:
number = 10 # Integer pi = 3.14 # Float greeting = "Hello!" # String numbers_list = [1, 2, 3, 4, 5] # List print(number, pi, greeting) print(numbers_list)
- Python supports various data types, including integers, floats, strings, and lists. Try experimenting with these:
Functions:
- Functions are blocks of code that perform a specific task. Define a function and call it:
def greet(name): return f"Hello, {name}!" print(greet("Bob"))
- Functions are blocks of code that perform a specific task. Define a function and call it:
Control Flow:
- Use
if
statements to control the flow of your program:age = 18 if age >= 18: print("You are an adult.") else: print("You are a minor.")
- Use
5. Practice and Experiment
- Try modifying the code snippets provided to see how changes affect the output.
- Explore more Python concepts such as loops, error handling, and file operations.
Conclusion
Congratulations on completing your first Python exercise! You’ve learned the basics of Python programming and written a simple script. Keep practicing and exploring more features of Python to build your programming skills.
Happy coding!