**Python Fundamentals: Variables, Data Types, and Operators

This lesson introduces the foundational elements of Python programming: variables, data types, and operators. You'll learn how to store and manipulate data, which are essential skills for any data scientist. By the end, you'll be able to write basic Python code that performs simple calculations and data handling tasks.

Learning Objectives

  • Define and use variables to store data in Python.
  • Identify and differentiate between common Python data types (integers, floats, strings, booleans).
  • Utilize arithmetic, comparison, and logical operators in Python expressions.
  • Understand the concept of type conversion and how to perform it.

Text-to-Speech

Listen to the lesson content

Lesson Content

Variables: Your Data Containers

Variables are named storage locations in your computer's memory that hold data. Think of them as labeled boxes where you can put different things (data). In Python, you don't need to declare the variable's type explicitly; Python figures it out based on the value you assign to it.

Example:

age = 30  # An integer
name = "Alice"  # A string
pi = 3.14159  # A float

print(age)
print(name)
print(pi) 

Here, age, name, and pi are variables. The values assigned to them (30, "Alice", and 3.14159) are the data the variables store.

Data Types: The Kind of Data

Python supports several built-in data types. Understanding these types is crucial because they determine what operations you can perform on your data.

  • 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 (e.g., "hello", "Python") - enclosed in single or double quotes.
  • Booleans (bool): Represent truth values: True or False.

Example:

num_students = 25  # int
price = 19.99  # float
course_name = "Data Science Basics" # str
is_active = True  # bool

print(type(num_students)) # Output: <class 'int'>
print(type(price)) # Output: <class 'float'>
print(type(course_name)) # Output: <class 'str'>
print(type(is_active)) # Output: <class 'bool'>

Operators: Working with Data

Operators are special symbols that perform operations on values. Python has several types of operators:

  • Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), ** (exponentiation), % (modulo - remainder).
  • Comparison Operators: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to).
  • Logical Operators: and (both are true), or (at least one is true), not (negates a boolean value).

Example:

a = 10
b = 3

print(a + b) # Output: 13
print(a / b) # Output: 3.3333333333333335
print(a % b) # Output: 1

print(a > b and b < 5) # Output: True
print(not(a < b)) # Output: True

Type Conversion: Changing Data Types

Sometimes, you need to change a variable's data type. Python allows you to convert between types using built-in functions:

  • int(): Converts to an integer (if possible).
  • float(): Converts to a float.
  • str(): Converts to a string.
  • bool(): Converts to a boolean (e.g., non-zero numbers become True, 0 becomes False).

Example:

number_string = "10"  # String
number_int = int(number_string)  # Convert to integer
print(type(number_int)) # Output: <class 'int'>
print(number_int + 5) # Output: 15

float_number = 3.14
int_number = int(float_number)
print(int_number)  #Output: 3
Progress
0%