No history yet

Python Basics

Getting Started with Python

Python is a popular programming language known for its clear and readable code. It's like a set of instructions you give to a computer. Before you can give any instructions, you need to install Python on your machine. Think of it as teaching your computer a new language.

Lesson image

You can download the latest version from the official Python website. The installation is straightforward and includes a program called IDLE, which is a simple environment where you can write and run your first lines of code. Once installed, you have a Python "interpreter" ready to go. The interpreter is a program that reads your Python code line by line and executes it.

Your First Program

It's a tradition in programming to start by making the computer say "Hello, World!". This simple task is a great way to confirm that your setup is working correctly.

print("Hello, World!")

In this line, print() is a function, which is a named block of code that performs a specific action. Here, its action is to display whatever is inside the parentheses on the screen. The text "Hello, World!" is called a string, which is just a sequence of characters. In Python, strings are always wrapped in quotes.

function

noun

A reusable block of code that performs a specific task when called.

Storing Information

Programs need to remember things. We use variables to store information. Think of a variable as a labeled box where you can put a piece of data. You give the box a name, and you can change what's inside it later.

# Storing a name (string)
user_name = "Alice"

# Storing an age (integer)
user_age = 30

# Storing a price (float)
item_price = 19.99

# Storing a state (boolean)
is_logged_in = True

Here, we created four variables. user_name holds a string, user_age holds an integer (a whole number), item_price holds a float (a number with a decimal), and is_logged_in holds a boolean (either True or False). Python automatically figures out the data type based on the value you assign.

Variable names can't start with a number and can't contain spaces. By convention, we use snake_case for names made of multiple words.

Making Decisions and Repeating Actions

Programs often need to make decisions or perform the same task multiple times. This is handled by control structures.

An if statement checks if a condition is true and runs a block of code only if it is. Imagine a program that greets an adult differently from a child.

age = 25

if age >= 18:
    print("Welcome, adult.")
else:
    print("You are a minor.")

# The indentation (the space before print) is crucial!
# It tells Python which code belongs to the if/else block.

To repeat actions, we use loops. A for loop is perfect for repeating a task a specific number of times. For example, to count from 0 to 4:

for i in range(5):
    print(i)

# This will print:
# 0
# 1
# 2
# 3
# 4

A while loop repeats as long as a condition is true. This is useful when you don't know exactly how many times you'll need to loop.

count = 0
while count < 3:
    print("Looping...")
    count = count + 1 # Increment the count to avoid an infinite loop

Creating Your Own Tools

As your programs grow, you'll find yourself writing the same bits of code over and over. Functions let you package up a block of code, give it a name, and reuse it whenever you need it. This makes your code more organized and easier to manage.

Let's create a simple function that greets a person by name.

# Define the function
def greet(name):
    print(f"Hello, {name}!")

# Call the function with different arguments
greet("Bob")
greet("Charlie")

We define the function using the def keyword. name is a parameter, a placeholder for the data we'll give the function when we call it. When we call greet("Bob"), the value "Bob" is passed in as the argument, and the function prints "Hello, Bob!". The f before the string creates an f-string, which lets us embed variable values directly inside the curly braces {}.

Functions can also perform a calculation and give back a result using the return keyword.

def add(a, b):
    return a + b

result = add(5, 3)
print(result) # This will print 8

Interacting with the User

So far, our data has been hard-coded into the program. To make programs interactive, we need to get input from the user and display output back to them. We've already seen the print() function for output.

To get input, we use the input() function. It prompts the user with a message, waits for them to type something and press Enter, and then returns what they typed as a string.

# Ask the user for their name
user_name = input("What is your name? ")

# Use the input in a greeting
print(f"Nice to meet you, {user_name}!")

One important detail: input() always returns a string. If you need a number, you have to convert it using int() or float().

age_string = input("How old are you? ")
age_number = int(age_string) # Convert the string to an integer

years_until_100 = 100 - age_number
print(f"You will be 100 in {years_until_100} years.")

Now let's test your understanding of these fundamental concepts.

Quiz Questions 1/6

What is the primary role of the Python interpreter?

Quiz Questions 2/6

After running the code item_price = 19.99, what is the data type of the item_price variable?

These are the building blocks of Python. Mastering them will prepare you for tackling more complex and exciting programming challenges.