Python Data Analytics for Beginners
Introduction to Python
Your First Steps in Code
Think of a programming language as a set of instructions a computer can understand. Python is popular because its instructions look a lot like plain English. The rules for how you write these instructions, like grammar in a sentence, are called syntax. Python's syntax is known for being clean and readable.
Let's start with the most traditional first program: getting the computer to say "Hello, World!". In Python, you use the print() function to display text on the screen. A function is just a named chunk of code that performs a specific task.
print("Hello, World!")
When you run this line of code, the text inside the quotation marks will appear as output. This is a basic output operation, where the program gives information back to you.
Storing Information
Programs need to remember things. We store information in variables, which are like labeled boxes where you can keep data. To create a variable and put something in it, you use the assignment operator, which is just an equals sign (=).
# Create a variable named 'greeting' and assign it a value
greeting = "Welcome to Python!"
# Print the value stored in the variable
print(greeting)
In this example, we created a variable named greeting and stored the text "Welcome to Python!" inside it. Then, we printed the contents of the variable. The name of the variable goes on the left of the =, and the value you want to store goes on the right.
Variables can hold different kinds of data. For now, we'll focus on three fundamental data types.
string
noun
A sequence of characters, used for text. You create strings by putting text inside single or double quotes.
user_name = "Alex"
integer
noun
A whole number, without any fractions or decimals.
items_in_cart = 5
float
noun
A number that has a decimal point. The name comes from "floating-point number."
price = 19.99
One of Python's handy features is that it automatically detects the data type when you assign a value to a variable. You don't have to declare it yourself.
You can also get information from the user with the input() function. This is a basic input operation.
name = input("What is your name? ")
print("Hello, " + name)
When you run the code above, the program will pause and wait for you to type something and press Enter. Whatever you type is stored in the
namevariable as a string.
Controlling the Flow
So far, our code runs straight from top to bottom. Control structures let us change that flow, allowing the program to make decisions or repeat actions.
The most common way to make a decision is with an if statement. It checks if a certain condition is true. If it is, a specific block of code runs. Notice the colon at the end of the if line and the indentation of the code below it. This indentation is crucial—it's how Python knows which code belongs to the if statement.
age = 20
if age >= 18:
print("You are old enough to vote.")
You can also provide an alternative action for when the condition is false using else.
age = 16
if age >= 18:
print("You are old enough to vote.")
else:
print("You are not old enough to vote yet.")
What if you need to repeat an action multiple times? That's what loops are for. A for loop is great for repeating a task a specific number of times. The range() function is often used here to generate a sequence of numbers.
# This loop will run 3 times.
# The variable `i` will be 0, then 1, then 2.
for i in range(3):
print("Looping...")
Another type of loop is the while loop. It continues to run as long as its condition remains true. It's important to make sure the condition will eventually become false, otherwise you'll create an infinite loop!
count = 1
while count <= 3:
print("The count is:", count)
count = count + 1 # This line prevents an infinite loop!
print("Loop finished.")
In this example, the loop prints the value of count, then increases it by one. Once count reaches 4, the condition count <= 3 is no longer true, and the loop stops.
In programming, what is the term for the set of rules that dictates how instructions must be written for a computer to understand them?
Which line of Python code correctly assigns the text "Hello" to a variable named message?
These are the absolute fundamentals of programming in Python. By combining variables, data types, and control structures, you can start to write simple but powerful scripts.