Skip to content

Step 2 | Python Basics

πŸš€ Getting Started with Python: Basic Concepts and Hands-On Examples

Section titled β€œπŸš€ 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.


Before jumping into Python basics, ensure you have a proper setup with a virtual environment.

Open a terminal and create a new project folder:

Terminal window
mkdir python_project # Create a new folder
cd python_project # Move into the folder
Terminal window
python -m venv myenv

This will create a virtual environment named myenv inside your project folder.

  • 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.

Use pip to install necessary Python packages inside your virtual environment:

Terminal window
pip install requests

To verify the installed packages:

Terminal window
pip list

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:

Terminal window
python hello.py

βœ… Expected Output:

Hello, World!

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 float
age = 25
price = 99.99
# String
name = "Alice"
# Boolean
is_python_fun = True
# List (ordered collection)
fruits = ["Apple", "Banana", "Cherry"]
# Dictionary (key-value pairs)
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Print values
print("Name:", name)
print("Age:", age)
print("Is Python fun?", is_python_fun)
print("Favorite fruits:", fruits)
print("Person Details:", person)

Run the script:

Terminal window
python basics.py

βœ… Expected Output:

Name: Alice
Age: 25
Is Python fun? True
Favorite fruits: ['Apple', 'Banana', 'Cherry']
Person Details: {'name': 'Alice', 'age': 25, 'city': 'New York'}

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:

Terminal window
python basics.py

βœ… Try entering your name and age to see how input works!


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 help in iterating over sequences like lists or performing repeated tasks.

# Using a for loop
for i in range(1, 6):
print(f"Number {i}")
# Using a while loop
count = 3
while count > 0:
print(f"Countdown: {count}")
count -= 1

βœ… This will print numbers using for and while loops.


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.


βœ… 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