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.
πΉ Setting Up Python and Virtual Environment
Section titled βπΉ 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
Section titled β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 folder
2οΈβ£ Create a Virtual Environment
Section titled β2οΈβ£ Create a Virtual Environmentβpython -m venv myenv
This will create a virtual environment named myenv
inside your project folder.
3οΈβ£ Activate the Virtual Environment
Section titled β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
Section titled β4οΈβ£ Install Required PackagesβUse pip
to install necessary Python packages inside your virtual environment:
pip install requests
To verify the installed packages:
pip list
πΉ Running Your First Python Program
Section titled βπΉ 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
Section titled βπΉ 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
Section titled βπΉ 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
Section titled βπΉ 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
Section titled βπΉ 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
Section titled βπΉ 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
Section titled βπ 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