Overview
This lecture provides a beginner-friendly introduction to Python programming, covering installation, basic syntax, variables, data types, operators, control structures, collections, loops, functions, and project ideas.
Getting Started with Python
- Download Python from python.org and install it on your system.
- Use IDEs like PyCharm or pythonanywhere.com for writing code.
- The first code prints text using
print("Hello World").
- Strings can be written in single or double quotes.
Basic Syntax and Variables
- Comments are written using
# and are ignored by Python.
- Variables store data and are assigned with
=.
- Variable names should be descriptive and follow Python naming rules.
- Variable types include string, integer, float, and boolean.
- Variables can change values after assignment.
Taking Input and Type Conversion
- Use
input() to get user data; input is always a string.
- Convert input to int (
int()), float (float()), or bool (bool()) when needed.
- Concatenate strings using
+, e.g., "Hello " + name.
Data Types and Operations
- Integer: whole numbers (e.g., 24); Float: decimals (e.g., 24.0).
- Boolean:
True or False values (case sensitive).
- String methods include
.upper(), .lower(), .find(), .replace().
- Data type conversion is needed for arithmetic with inputs.
Operators
- Arithmetic:
+ (add), - (subtract), * (multiply), / (divide), // (integer divide), % (modulo), ** (power).
- Comparison:
>, <, >=, <=, ==, !=.
- Logical:
and, or, not.***
Control Structures
- Use
if, elif, else for conditional logic.
- Code blocks require indentation (typically 4 spaces).
- Nested conditions determine code flow.
Loops
while loops execute while a condition is true; remember to update loop variables.
for loops iterate over sequences (like range() or lists).
- Use
break to exit loops and continue to skip to the next iteration.
Collections: List, Tuple, Set, Dictionary
- List: ordered, mutable collection (
[95, 98, 97]); supports indexing and slicing.
- Tuple: ordered, immutable collection (
(95, 98, 97)).
- Set: unordered, unique elements (
{95, 98, 97}).
- Dictionary: key-value pairs (
{"english":95, "chemistry":98}).
Functions
- Define functions with
def function_name(parameters):.
- Functions can have default parameter values.
- Use
return to provide output from a function.
- Use modules (like
math) with import statements.
Project Ideas and Practice
- Mini-project: build a calculator using input, operators, and conditionals.
- Practice using loops to print patterns and process collections.
Key Terms & Definitions
- Variable β a named location in memory to store data.
- String β a sequence of characters (text).
- Boolean β a type with values
True or False.
- List β an ordered, mutable collection of items.
- Tuple β an ordered, immutable collection of items.
- Set β an unordered collection of unique items.
- Dictionary β a collection of key-value pairs.
- Function β a reusable block of code defined with
def.
- Module β a file containing Python definitions and statements.
Action Items / Next Steps
- Practice defining variables and using basic data types.
- Try both input and output operations.
- Complete provided exercises on variables and input.
- Write a calculator and pattern-printing program.
- Review list, tuple, set, and dictionary operations.
- Explore the
math module and try writing your own functions.