DEV Community

Chinyere Unamba
Chinyere Unamba Subscriber

Posted on • Originally published at Medium on

Getting Started with Python — Part 2

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!") 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, World! 
Enter fullscreen mode Exit fullscreen mode

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") 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

And with strings:

first_name = "Jane" last_name = "Doe" full_name = first_name + " " + last_name print(full_name) 
Enter fullscreen mode Exit fullscreen mode

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.") 
Enter fullscreen mode Exit fullscreen mode

Loops Made Simple

For Loop

for i in range(1, 6): print(i) 
Enter fullscreen mode Exit fullscreen mode

This prints numbers 1 to 5.

While Loop

count = 0 while count < 3: print("Counting...", count) count += 1 
Enter fullscreen mode Exit fullscreen mode

Functions — Your Reusable Helpers

Functions help you organize and reuse code:

def greet(name): print(f"Hello, {name}!") greet("Chinyere") 
Enter fullscreen mode Exit fullscreen mode

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}") 
Enter fullscreen mode Exit fullscreen mode

Taking Input from Users

name = input("What is your name? ") print(f"Welcome, {name}!") 
Enter fullscreen mode Exit fullscreen mode

For numbers:

age = int(input("How old are you? ")) print(f"In 5 years, you will be {age + 5}.") 
Enter fullscreen mode Exit fullscreen mode

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!") 
Enter fullscreen mode Exit fullscreen mode

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!

See part 1.

Top comments (0)