No history yet

Python Fundamentals Review

Python Syntax Refresher

In Python, you don't need to declare a variable's type. The interpreter figures it out when you assign a value. Just pick a name and use the equals sign.

user_age = 30
pi_approx = 3.14159
is_learning = True
greeting_message = "Hello, Python!"

For naming, Python has a style guide called PEP 8 that recommends using snake_case for variable names. This means all lowercase letters, with words separated by underscores. It's not a rule the computer enforces, but it's a strong convention that makes code easier for humans to read.

Core Operators

Python's operators will feel familiar. For math, you have the standard arithmetic operators, plus a few handy extras like exponentiation (**), floor division (//), and modulus (%).

Floor division (//) always rounds the result down to the nearest whole number, while modulus (%) gives you the remainder of a division.

Comparison operators (==, !=, >, <) work as you'd expect, evaluating to a True or False boolean value. For combining boolean expressions, Python uses plain English words: and, or, and not.

# Arithmetic
quotient = 10 / 3      # Result: 3.333...
floor_div = 10 // 3   # Result: 3
remainder = 10 % 3      # Result: 1
power = 2 ** 4          # Result: 16 (2*2*2*2)

# Comparison
is_equal = (power == 15)  # Result: False

# Logical
is_active = True
has_permission = False

can_access_system = is_active and has_permission # Result: False
can_see_dashboard = is_active or has_permission  # Result: True
is_not_active = not is_active                  # Result: False

Fundamental Data Types

Python's core data types are straightforward. You have integers (int), floating-point numbers (float), booleans (bool), and strings (str). An important concept for these basic types is immutability. This means that once a value is assigned, the object holding that value cannot be changed. Any operation that appears to modify it, like adding to a number or concatenating a string, actually creates a new object in memory.

Because you don't declare types, sometimes you need to explicitly convert data from one type to another. This is common when working with user input, which always comes in as a string.

You can use the built-in functions int(), float(), str(), and bool() to perform these conversions. Be careful, though. Trying to convert a string that isn't a valid number into an int or float will cause your program to crash with an error.

number_as_string = "42"

# Convert string to integer
actual_number = int(number_as_string)

# Perform math
result = actual_number + 8  # Result: 50

# Convert integer back to string for concatenation
message = "The result is: " + str(result) 
# Result: "The result is: 50"

Communicating with the User

To display information, you use the print() function. While you can combine strings with the + operator, a more modern and readable approach is to use f-strings. An f-string lets you embed expressions directly inside a string literal.

name = "Alex"
age = 28

# Using an f-string to format the output
print(f"User's name is {name} and they are {age} years old.")

# You can even put expressions inside the curly braces
print(f"In 5 years, {name} will be {age + 5} years old.")

To get data from a user, you use the input() function. It takes an optional string to use as a prompt. Remember, input() always returns the user's entry as a string, so you'll often need to convert it to the appropriate type before using it in calculations.

# Prompt the user for their birth year
birth_year_str = input("What year were you born? ")

# Convert the input string to an integer
birth_year = int(birth_year_str)

# Calculate approximate age
current_year = 2024
age = current_year - birth_year

# Print a formatted message
print(f"You are approximately {age} years old.")

Let's check your understanding of these core concepts.

Quiz Questions 1/6

What is the result of the following Python expression? 10 // 3

Quiz Questions 2/6

According to the PEP 8 style guide, which is the preferred way to name a variable that stores a user's first name?

With these fundamentals covered, you're ready to move on to Python's more complex data structures.