Getting Started with Python — Part 2
Welcome back! 🎉 If you’ve completed Part 1 , you’ve installed Python, set up your text editor, and are ready to write some actual programs. This part builds on that by introducing Python syntax, basic programming concepts, and your first mini-project.
Photo by Artturi Jalli on Unsplash
Writing Your First Python Program
1. Open VS Code
- Launch Visual Studio Code.
- Open a new folder or workspace where you want to save your Python files.
2. Create a Python File
- Go to File → New File.
- Save it as hello.py (all Python files end with .py).
3. Write Your First Code
print("Hello, World!")
Save the file.
4. Run the Program
- Open the terminal in VS Code (Ctrl + backtick).
- Ensure you are in the correct directory where the file is saved.
Run:
python hello.py
Output:
Hello, World!
Congratulations! You’ve just written and executed your first Python program! 🎉
Understanding Python Syntax
Python is loved for its clean and simple syntax. Here are key basics:
Indentation
Unlike many other languages that use {} braces, Python uses indentation to define code blocks.
if True: print("This line is indented")
Comments
Comments are notes to yourself or others. They are ignored by Python but make your code easier to understand.
# This is a comment print("Hello") # This is also a comment
Variables and Data Types
Think of variables as containers for storing information.
name = "Chinyere" # String year = 2025 # Integer height = 5.6 # Float is_student = True # Boolean
Basic Operations
Python makes math and string operations easy:
x = 10 y = 3 print(x + y) # Addition print(x - y) # Subtraction print(x * y) # Multiplication print(x / y) # Division
And with strings:
first_name = "Jane" last_name = "Doe" full_name = first_name + " " + last_name print(full_name)
Making Decisions with if Statements
Want your program to respond differently based on a condition? Use if:
age = 18 if age >= 18: print("You can vote!") else: print("You are too young to vote.")
Loops Made Simple
For Loop
for i in range(1, 6): print(i)
This prints numbers 1 to 5.
While Loop
count = 0 while count < 3: print("Counting...", count) count += 1
Functions — Your Reusable Helpers
Functions help you organize and reuse code:
def greet(name): print(f"Hello, {name}!") greet("Chinyere")
Playing with Modules
Modules are like toolkits. Try the random module:
import random number = random.randint(1, 10) print(f"Your random number is {number}")
Taking Input from Users
name = input("What is your name? ") print(f"Welcome, {name}!")
For numbers:
age = int(input("How old are you? ")) print(f"In 5 years, you will be {age + 5}.")
Mini Project: Number Guessing Game
Put it all together:
import random secret_number = random.randint(1, 10) guess = 0 print("I'm thinking of a number between 1 and 10.") while guess != secret_number: guess = int(input("Take a guess: ")) if guess < secret_number: print("Too low! Try again.") elif guess > secret_number: print("Too high! Try again.") else: print("Congratulations! You guessed it!")
Wrap-Up
In this part, you’ve learned to:
- Write and run Python scripts
- Use variables, data types, and basic operations
- Make decisions with conditions
- Use loops for repetition
- Write and use functions
- Play with built-in modules
- Get user input
- Build your first mini-project 🎮
In Part 3 , we’ll explore lists, dictionaries, and error handling to make your programs even more powerful!
Top comments (0)