LECTURE: Python Programming Foundations
- Prerequisites: None—this is your starting
point!
- Time to Complete: 15-20 minutes
What you'll be able to do:
- Explain Python variables, data types,
functions, and file operations.
- Apply basic input/output operations in Python.
- Write simple Python programs using functions
and file handling.
Introduction: What is Python and Why
Should You Care?
Core Definition:
Python is a
high-level programming language that is easy to read and write. It’s used for
web development, data science, automation, and more.
A Simple Analogy:
Think of Python like
LEGO blocks. You can combine simple pieces to build complex structures.
Limitation:
Not all “LEGO” pieces
are built-in; sometimes you need to install extra packages.
Why This Matters to You:
- Python is beginner-friendly and versatile.
Learning Python opens doors to real-world applications such as automating
tasks, analyzing data, and building web apps.
- Real-world context: Companies like Google,
Instagram, and Netflix use Python in various ways.
The Foundation: Core Concepts Explained
Concept A: Variables
Definition:
- Variables store data in your program, like a
labeled jar for ingredients.
Key characteristics:
- Name: Identifier to access stored data
- Value: Actual data stored
- Type: Kind of data (integer, string, etc.)
Example:
name = "Amit"
age = 21
print(name, age)
Common confusion:
- Variable names cannot start with a number or
use special characters except _.
Concept B: Data Types
Definition:
- Data types define what kind of value a
variable can hold.
Key types:
Integer (int): Whole numbers → age = 21
String (str): Text → name =
"Amit"
Float (float): Decimal numbers → height
= 5.7
Boolean (bool): True or False →
is_student = True
Concrete example:
temperature = 36.6 # float
is_adult = True # boolean
Remember: Strings must be in quotes;
numbers don’t.
Concept C: Input & Output
Definition:
- Input: Get information from the user
- Output: Show results to the user
Example:
name = input("Enter your name:
")
print("Hello,", name)
Common confusion:
- input() always returns a string. Convert if
needed:
age = int(input("Enter your age:
"))
Concept D: Functions
Definition:
A function is a reusable block of code
that performs a task.
Key characteristics:
- Name: Used to call the function
- Parameters: Inputs to the function
- Return value: Output of the function
Example:
def greet(name):
return "Hello, " + name
print(greet("Amit"))
Common confusion:
- Don’t forget to return values if you need
them.
Concept E: Virtual Environment
(virtualenv)
Definition:
- A virtual environment isolates your Python
project and its dependencies from other projects.
Example:
# Create a virtual environment
python -m venv myenv
# Activate it (Windows)
myenv\Scripts\activate
# Activate it (Mac/Linux)
source myenv/bin/activate
Remember:
- Each project can have its own packages without
conflicts.
Concept F: File Reading & Writing
Basics
Definition:
- Python can read from and write to files to
store or retrieve information.
Example:
# Writing to a file
with open("data.txt",
"w") as f:
f.write("Hello, world!")
# Reading from a file
with open("data.txt",
"r") as f:
content = f.read()
print(content)
Common confusion:
- Use with open to automatically close files and
avoid errors.
Seeing It in Action: Worked Examples
Example 1: Variables & Input/Output
Scenario: Ask the user’s name and age,
then display a message.
name = input("Enter your name:
")
age = int(input("Enter your age:
"))
print(f"Hello {name}, you are
{age} years old!")
What just happened:
- Variables stored user input, and formatted
output displayed it.
Example 2: Functions & File Writing
Scenario: Save a greeting message to a
file using a function.
def create_greeting(name):
return f"Hello {name}, welcome!"
name = "Amit"
message = create_greeting(name)
with open("greeting.txt",
"w") as f:
f.write(message)
Key lesson:
- Functions make code reusable; file operations
save data.
Example 3: Virtual Environment Setup
Scenario: Isolate a project to avoid
package conflicts.
python -m venv project_env
source project_env/bin/activate
pip install requests
Key lesson:
- Virtual environments keep projects clean and
organized.
Common Pitfalls
- Mistake: Forgetting to convert input to
integer.
- Problem: Calculation errors
- Fix: int(input("Enter a number: "))
- Mistake: Forgetting return in functions.
- Problem: Output is None
- Fix: Always return values if needed
- Mistake: Not closing files.
- Problem: Data may not save correctly
- Fix: Use with open(...)
Your Turn: Practice &
Self-Assessment
Practice Task:
Ask the user for their name and
favorite number.
Save a message in a file: "Hello
<name>, your favorite number is <number>".
Read the file and display the message.
Check Your Understanding:
- What happens if you don’t convert input to
integer?
- How do virtual environments help in project
management?
- Why should you use functions for repetitive
tasks?
Consolidation: Key Takeaways & Next
Steps
Core concepts recap:
- Variables store information.
- Data types define the kind of information.
- Input/Output interacts with the user.
- Functions make code reusable.
- Virtual environments manage dependencies.
- File operations read and write persistent
data.
Mental Model Check:
- Think of Python as your toolkit. Each concept
(variables, functions, files) is a tool to build real applications.
Next Steps:
Practice writing Python programs:
- Experiment with variables, data types, and
basic I/O to solidify your understanding.
Explore functions and file handling:
- Try creating small programs that use functions
and read/write files to see real applications.