No history yet

Python Basics

Variables and Data Types

Think of a variable as a labeled box where you can store information. You give the box a name, and you can put something inside it. In Python, you can store different kinds of information, and these are called data types.

The most common data types you'll encounter are:

  • Integers: Whole numbers, like 10, -5, or 1000.
  • Floats: Numbers with decimal points, like 3.14 or -0.5.
  • Strings: Plain text, which you wrap in single or double quotes.
  • Booleans: These represent truth values and can only be True or False.
# An integer for whole numbers
user_age = 25

# A float for decimal numbers
product_price = 49.95

# A string for text, using double quotes
user_name = "John Doe"

# A boolean for true/false values
is_logged_in = True

Python is dynamically typed. This means you don't have to explicitly declare a variable's type. Just assign a value, and Python figures it out for you. You can even change the type of data stored in a variable later on.

Making Decisions and Repeating Actions

Programs rarely run straight from top to bottom. They need to make decisions and perform repetitive tasks. Control structures allow you to direct the flow of your code.

To make decisions, you use if statements. An if statement checks if a condition is true and runs a block of code only if it is. You can add an elif (else if) to check other conditions, and an else to run code if none of the conditions are met.

temperature = 75

if temperature > 80:
    print("It's a hot day!")
elif temperature < 60:
    print("It's a bit chilly.")
else:
    print("The weather is pleasant.")

When you need to repeat an action, you use loops. The for loop is perfect for iterating over a sequence of items. For example, you can loop through a range of numbers.

# This loop will run 5 times, for numbers 0 through 4
for i in range(5):
    print(f"This is loop number {i}")

The while loop is used to repeat a block of code as long as a certain condition remains true. It's useful when you don't know in advance how many times you need to loop.

count = 0
while count < 3:
    print(f"Count is {count}")
    count = count + 1 # Increment the count to avoid an infinite loop

Functions and Modules

As your programs get bigger, you'll find yourself writing the same pieces of code again and again. Functions help you package blocks of code so you can reuse them easily. You define a function once and then "call" it whenever you need it.

Functions make your code organized, reusable, and easier to read.

To create a function, you use the def keyword, give the function a name, and specify any parameters it needs inside the parentheses. The code inside the function is indented.

# Define a function that greets a user
def greet(name):
    message = f"Hello, {name}!"
    return message

# Call the function and print the result
welcome_message = greet("Alice")
print(welcome_message)

Python also has a vast standard library of pre-written code organized into modules. To use a function from a module, you first have to import it. For example, the math module contains many useful mathematical functions.

import math

# Use the sqrt function from the math module
square_root = math.sqrt(16)

print(f"The square root of 16 is {square_root}")

Basic Input and Output

So far, we've only been displaying information. A program is much more useful when it can interact with a user. The simplest way to display information is with the print() function, which you've already seen.

To get information from the user, you can use the input() function. It prompts the user to enter some text and returns their input as a string.

# Ask the user for their name
user_name = input("Please enter your name: ")

# Greet the user with their name
print(f"Welcome to Python, {user_name}!")

One important detail: the input() function always returns a string. If you need to treat the input as a number, you have to convert it first using functions like int() or float().

age_string = input("How old are you? ")
age_number = int(age_string)

next_year_age = age_number + 1
print(f"Next year, you will be {next_year_age} years old.")

Now that you've covered the fundamental building blocks, let's test your understanding.

Quiz Questions 1/6

What is the data type of the value 10.5 in Python?

Quiz Questions 2/6

What will the following Python code print?

x = 5
if x > 10:
    print("A")
elif x > 3:
    print("B")
else:
    print("C")

These core concepts—variables, control structures, functions, and I/O—form the foundation of almost everything you will do in Python.