Ace Your Intro Python Interview
Python Basics
The Building Blocks of Python
Every language, human or computer, has a set of rules for how to structure sentences. In programming, this is called syntax. Python is known for its clean and readable syntax, which makes it a great language for beginners. It often resembles plain English.
One key rule in Python is that indentation matters. Unlike many other languages that use brackets to define blocks of code, Python uses whitespace. This enforces a clean, organized layout.
Let's start with the classic first program: printing "Hello, World!" to the screen. It's a simple, one-line command that shows how straightforward Python can be.
print("Hello, World!")
Data Types
Programs work with information, or data. Python needs to know what kind of data it's handling. Is it a number? A piece of text? A simple yes or no? These categories are called data types.
Think of variables as labels for boxes that hold data. You can store a number in a box labeled age or text in a box labeled name.
# An integer (whole number)
age = 30
# A float (number with a decimal)
price = 19.99
# A string (text)
name = "Alice"
# A boolean (True or False)
is_active = True
Here are the most common basic data types you'll encounter:
| Data Type | Description | Example |
|---|---|---|
int | Integer, for whole numbers. | 42 |
float | Floating-point number, for decimals. | 3.14 |
str | String, for text. Enclosed in quotes. | "Hello" |
bool | Boolean, for logical True/False values. | True |
Controlling the Flow
A program rarely runs straight from top to bottom. Often, you need it to make decisions or repeat actions. Control structures let you direct the flow of your code.
Conditional statements use if, elif (else if), and else to run certain blocks of code only when a specific condition is met. It’s like telling the computer, "If this is true, do this; otherwise, do that."
temperature = 25
if temperature > 30:
print("It's a hot day!")
elif temperature > 20:
print("It's a pleasant day.")
else:
print("It's a bit chilly.")
Loops are for repetition. A for loop is great for when you want to do something for each item in a list. A while loop is used to repeat a task as long as a certain condition remains true.
# A for loop that prints each fruit
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# A while loop that counts down from 3
count = 3
while count > 0:
print(count)
count = count - 1
print("Liftoff!")
Reusable Code with Functions
If you find yourself writing the same piece of code over and over, it's time to create a function. A function is a named, reusable block of code that performs a specific task. Think of it like a recipe: you define it once, and then you can "call" it whenever you need to make that dish.
Functions can take inputs, called arguments, and can produce an output, called a return value.
# Define a function that greets someone
def greet(name):
return f"Hello, {name}!"
# Call the function and print its output
message = greet("Bob")
print(message)
Using functions makes your code more organized, easier to read, and simpler to debug.
Handling Errors
Sometimes, code doesn't run as expected. You might try to divide a number by zero or access a file that doesn't exist. These situations cause errors, or exceptions, which can crash your program.
Instead of letting the program crash, you can anticipate and manage these errors gracefully using a try...except block. You put the risky code in the try block and the code to handle a potential error in the except block.
try:
# This line will cause a ZeroDivisionError
result = 10 / 0
print(result)
except ZeroDivisionError:
# This block runs if the error occurs
print("You can't divide by zero!")
This prevents the program from stopping and allows you to provide a helpful message or take another course of action.
Ready to check your understanding? Let's tackle a few questions on these core concepts.
What is "syntax" in the context of a programming language?
In Python, what is the primary purpose of a variable?
With these fundamentals—syntax, data types, control flow, functions, and error handling—you have the essential tools to start building simple yet powerful programs in Python.