Step 2 | Python Basics
🚀 Getting Started with Python: Basic Concepts and Hands-On Examples
Python is one of the most beginner-friendly programming languages, widely used for web development, data science, automation, and AI. In this blog, we’ll cover essential Python basics to help you get started with programming.
🔹 Setting Up Python and Virtual Environment
Before jumping into Python basics, ensure you have a proper setup with a virtual environment.
1️⃣ Creating a Project Folder
Open a terminal and create a new project folder:
mkdir python_project # Create a new foldercd python_project # Move into the folder2️⃣ Create a Virtual Environment
python -m venv myenvThis will create a virtual environment named myenv inside your project folder.
3️⃣ Activate the Virtual Environment
- For Windows (Command Prompt):
Terminal window myenv\Scripts\activate - For Windows (PowerShell):
Terminal window myenv\Scripts\Activate.ps1 - For macOS/Linux:
Terminal window source myenv/bin/activate
Once activated, your terminal will show (myenv) before the prompt.
4️⃣ Install Required Packages
Use pip to install necessary Python packages inside your virtual environment:
pip install requestsTo verify the installed packages:
pip list🔹 Running Your First Python Program
Create a new Python file named hello.py in the project folder and add the following code:
print("Hello, World!")Run the script inside the activated virtual environment:
python hello.py✅ Expected Output:
Hello, World!🔹 Variables and Data Types
Python supports multiple data types like integers, strings, lists, and dictionaries.
Create a new Python script basics.py and add the following code:
# Integer and floatage = 25price = 99.99
# Stringname = "Alice"
# Booleanis_python_fun = True
# List (ordered collection)fruits = ["Apple", "Banana", "Cherry"]
# Dictionary (key-value pairs)person = { "name": "Alice", "age": 25, "city": "New York"}
# Print valuesprint("Name:", name)print("Age:", age)print("Is Python fun?", is_python_fun)print("Favorite fruits:", fruits)print("Person Details:", person)Run the script:
python basics.py✅ Expected Output:
Name: AliceAge: 25Is Python fun? TrueFavorite fruits: ['Apple', 'Banana', 'Cherry']Person Details: {'name': 'Alice', 'age': 25, 'city': 'New York'}🔹 Taking User Input
Modify basics.py to allow user input:
name = input("Enter your name: ")age = int(input("Enter your age: "))
print(f"Hello, {name}! You are {age} years old.")Run:
python basics.py✅ Try entering your name and age to see how input works!
🔹 Conditional Statements in Python
Python allows control flow using if-else conditions.
num = int(input("Enter a number: "))
if num > 0: print("The number is positive.")elif num < 0: print("The number is negative.")else: print("The number is zero.")✅ This program checks if the number is positive, negative, or zero.
🔹 Loops in Python
Loops help in iterating over sequences like lists or performing repeated tasks.
# Using a for loopfor i in range(1, 6): print(f"Number {i}")
# Using a while loopcount = 3while count > 0: print(f"Countdown: {count}") count -= 1✅ This will print numbers using for and while loops.
🔹 Functions in Python
Functions allow us to reuse code efficiently.
def greet(name): return f"Hello, {name}!"
user_name = input("Enter your name: ")message = greet(user_name)print(message)✅ This defines a function greet() and calls it with user input.
🚀 Summary of Steps
✅ Set up Python and a virtual environment
✅ Ran a Python script (hello.py)
✅ Worked with variables and data types
✅ Used user input (input())
✅ Learned about conditional statements (if-else)
✅ Implemented loops (for, while)
✅ Created functions in Python