Python Programming Basics
- Python can be used as a calculator for basic arithmetic operations.
- Variables are assigned using the
=
operator. - DataFrames are used to store tabular data using the pandas library.
- Understanding different object types in Python is crucial for data manipulation.
- Logical and comparison operators help in making decisions.
- Loops and functions are essential for repetitive tasks and code organization.
Welcome to Python programming! This guide will introduce you to the basics of Python, starting with simple calculations, then moving on to assigning variables, working with DataFrames, and exploring different object types.
Introduction
Using Python as a Calculator
Python can be used to perform basic arithmetic operations just like a calculator.
Assigning Variables
In Python, you can assign values to variables using the =
operator.
DataFrames
DataFrames are a fundamental data structure in Python, used to store tabular data. Each column in a DataFrame is a series of values of the same type. This structure allows for efficient data manipulation and analysis. DataFrames are created using the pandas library.
Objects
Basic Types
Scalars
Lists
Combine objects of the same type in a list
Access items using square brackets
Dictionaries
Combine objects of different types in a dictionary
Dictionaries can contain dictionaries (nested dictionaries)
Subset dictionaries using square brackets
Access dictionary items with square brackets
DataFrames
A special type of dictionary, where each item is a series of the same length
Subset with square brackets
Access columns with square brackets or dot notation. Both methods return a series
Functions
Functions are blocks of code
Functions may be called to execute the code they contain
Arguments extend function utility
Conditions
Comparison operators
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 4 |
True |
< |
Less than | 3 < 5 |
True |
> |
Greater than | 3 > 5 |
False |
<= |
Less than or equal to | 3 <= 3 |
True |
>= |
Greater than or equal to | 3 >= 5 |
False |
Logical operators
Operator | Description | Example | Result |
---|---|---|---|
& |
Element-wise AND | [True, False] & [True, True] |
[True, False] |
| |
Element-wise OR | [True, False] | [False, True] |
[True, True] |
not |
NOT | not True |
False |
in |
Checks if elements in one list are present in another list | 2 in [1, 2, 3] |
True |
Examples
if, elif, else
Data wrangling
With the foundational knowledge we’ve covered, we can now begin to manipulate our DataFrames effectively.
Filtering rows
Use a boolean mask
Creating columns
Loops
Loops are an essential concept in programming, allowing you to automate repetitive tasks efficiently. They enable you to execute a block of code multiple times, which can save time and reduce errors in your code.
For loops
Do something for each item in an iterable (list/dictionary)
Can also loop through by index using range()
A common pattern is to create an empty list to hold the results from your loop
While loops
Continue looping while a certain condition is met (beware infinite loops!)
Exercises
Now it’s time to practice what you’ve learned. Try the following exercises. Hints are available if you get stuck, and your answers will be graded.
Exercise 1: Basic Arithmetic
Calculate the sum of 15 and 25.
Remember to use the +
operator for addition.
15 + 25
Simply add the two numbers:
15 + 25
Exercise 2: Assigning Variables
Assign the value 100 to a variable named a
and the value 200 to a variable named b
. Then calculate their sum.
Use the =
operator to assign values to variables.
= 100
a = 200 b
Assign the values and calculate the sum:
= 100
a = 200
b + b a
Exercise 3: Logical Operators
Check if the number 5 is greater than 3 and less than 10.
Use the and
operator to combine two logical conditions.
5 > 3 and 5 < 10
Combine the two conditions using the and
operator:
5 > 3 and 5 < 10
Exercise 4: For Loop
Write a for loop to create a list containing the numbers 1 to 5.
Use the append()
method to add elements to a list.
# empty list
= []
result
# append `1` to the empty list using `append()`
1) result.append(
Use append()
to grow the list:
= []
result for i in range(1, 6):
result.append(i) result
Exercise 5: While Loop
Write a while loop to create a list containing the numbers 1 to 5.
Increment the value of x
by 1 in each iteration.
= 1
x
# the loop will complete when the value of `x` reaches 6
while x <= 5:
+= 1 x
Write the while loop to increment x
and grow the list:
= []
result = 1
x while x <= 5:
result.append(x)+= 1
x result
Exercise 6: DataFrames - filter rows
Filter this DataFrame for people aged 30 or older.
Use square brackets to filter rows.
# Filter for rows where name is "Alice"
"Name"] == "Alice"] df[df[
Filter the DataFrame using square brackets:
"Age"] >= 30] df[df[