Python for Data Analysis
Python Basics
The Rules of the Road
Every language has rules, and programming languages are no different. In Python, the rules, or syntax, are designed to be clean and readable. One of the most important rules is indentation. Where other languages might use brackets or keywords to mark a block of code, Python uses whitespace. This might seem strange at first, but it forces code to be organized and easy to read.
Let's start with the classic first program: printing "Hello, World!" to the screen. It's a tradition for a reason. It shows you the simplest way to make a computer do something.
print("Hello, World!")
That's it. The print() function simply displays whatever you put inside the parentheses. No complex setup, just a straightforward command.
Storing Information
To do anything useful, programs need to remember information. We store this information in variables. Think of a variable as a labeled box where you can keep a piece of data. You give the box a name and put something inside it.
# Assigning the value 10 to a variable named 'score'
score = 10
# Now we can use the variable's name to get its value
print(score)
The information we store comes in different forms, or data types. For now, we'll focus on three of the most common ones: integers, floats, and strings.
integer
noun
A whole number, without any decimal points. It can be positive, negative, or zero.
float
noun
A number that has a decimal point. The name comes from "floating-point number."
string
noun
A sequence of characters, like text. In Python, we create strings by putting characters inside single or double quotes.
Python is smart enough to figure out the data type on its own when you create a variable. You don't have to declare it explicitly.
# An integer
player_age = 28
# A float
average_score = 85.5
# A string
player_name = "Alex"
# We can check the type of each variable
print(type(player_age))
print(type(average_score))
print(type(player_name))
Choosing meaningful variable names makes your code much easier to understand.
player_nameis clearer thanpn.
Controlling the Flow
So far, our code runs straight from top to bottom. But what if we want it to make decisions or repeat actions? That's where control structures come in. They direct the flow of your program.
The most common way to make a decision is with an if statement. It checks if a condition is true, and if it is, it runs a specific block of code. You can also provide alternative paths with elif (else if) and else.
temperature = 75
if temperature > 80:
print("It's a hot day!")
elif temperature < 60:
print("Better bring a jacket.")
else:
print("The weather is perfect.")
# Notice the colon and the indentation!
When you need to do something over and over, you use a loop. A for loop is great for when you want to repeat an action a specific number of times or for each item in a collection.
# This loop will run 5 times (for 0, 1, 2, 3, 4)
for i in range(5):
print("This is loop number", i)
Another type of loop is the while loop. It keeps repeating 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.
countdown = 3
while countdown > 0:
print(countdown)
countdown = countdown - 1 # Decrease countdown by 1
print("Liftoff!")
With variables to store data and control structures to direct the flow, you have the basic building blocks for writing powerful Python programs.
Time to check your understanding of these core concepts.
What does Python use to define the scope of a block of code, such as the body of a loop or an if statement?
Consider the following code: count = 5. What is the data type of the count variable?
These fundamentals are the foundation for everything else you'll do in programming. Getting comfortable with them is the first big step.