Python Fundamentals

This lesson introduces the fundamental building blocks of Python programming: variables, data types, and operators. You'll learn how to store and manipulate data, perform basic calculations, and understand the different types of information Python can handle. By the end of this lesson, you'll be able to write simple Python programs that perform basic operations.

Learning Objectives

  • Define and use variables to store different types of data.
  • Identify and differentiate between common Python data types (integers, floats, strings, booleans).
  • Utilize arithmetic, comparison, and logical operators in Python expressions.
  • Write simple Python programs that perform calculations and manipulate data.

Text-to-Speech

Listen to the lesson content

Lesson Content

Variables: Your Data Containers

A variable is like a named container that holds a value. In Python, you create a variable by giving it a name and assigning a value to it using the = operator. Variable names can contain letters, numbers, and underscores, but they cannot start with a number. Python is dynamically typed, meaning you don't need to declare the data type explicitly; it's inferred from the value you assign.

Example:

age = 30  # integer variable
name = "Alice"  # string variable
pi = 3.14159  # float variable
is_student = True # boolean variable

Data Types: The Kinds of Values

Python has several built-in data types. Understanding these is crucial.

  • Integers (int): Whole numbers (e.g., 10, -5, 0).
  • Floats (float): Numbers with decimal points (e.g., 3.14, -2.5).
  • Strings (str): Sequences of characters enclosed in single or double quotes (e.g., 'hello', "Python").
  • Booleans (bool): Represent truth values: True or False.

Example:

num_apples = 5         # int
price = 2.99           # float
message = "Hello, World!" # str
is_active = True       # bool

Operators: Doing the Work

Operators are special symbols that perform operations on values or variables. We'll cover:

  • Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), // (floor division - rounds down to the nearest whole number), % (modulo - gives the remainder), ** (exponentiation).
  • Comparison Operators: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).
  • Logical Operators: and (returns True if both operands are true), or (returns True if at least one operand is true), not (inverts the boolean value).

Example:

a = 10
b = 3

print(a + b)     # 13
print(a / b)     # 3.3333333333333335
print(a // b)    # 3 (floor division)
print(a % b)     # 1 (modulo)

print(a > b and b < 5)  # True
print(a == 10 or b == 1) # True
Progress
0%