Python Basics
In this markdown we will work through the basics of Python with a focus on the aspects that are important for data science and machine learning.
Working with types
In Python, we will be working with lots of different types of data. Sometimes that data will be numerical:
Other times it will be a text string such as a name:
More advanced cases will have lists of elements such as many names:
Python has many different types like this. Knowing the type of your data is the first step. Let's look at some examples:
Numbers
Numbers are the simplest type we will work with. Simply make a variable name and assign a number value. Common types of numbers are:
-
int: Integers are whole numbers without any fractional part. For example, 1, -5, 1000 are all integers.
-
float: Floating-Point Numbers are numbers with a fractional part. They can represent both whole numbers and numbers with decimal places. For example, 3.14, -2.5, 0.75 are all floating-point numbers.
Addition:
You can add two numbers together to output number3:
Strings
Strings are very easy to use in python. You can just use quotes to create them.
Concatenation:
To combine two strings you simply add them together.
Lists
Python has a simple type for multiple values called a list.
Adding Two Lists:
Adding two lists together creates a new list combining the two.
Dictionaries
A dictionary type stores "keys" and "values", and allows you to look up values using their corresponding keys. You can have as many keys and values as you want, and they can be of most of the types that we have seen so far.
To access a value of the dictionary, you use the square bracket notation with the key that you want to access.
Adding Values to a Dictionary
You can also add a new key to the dictionary by setting its value.
Dates
For date types, the datetime library is oftentimes useful
import datetime
date1 = datetime.datetime.now()
print(date1.day) # 16
print(date1.month) # 06
print(date1.year) # 2023
If statements
if statements check for a condition and run the code if it is true. In Python you need to indent the code under the if statement otherwise it will not run.
number3 = 10 + 75.0
if number3 > 50:
print("number is greater than 50")
if number3 > 100:
print("number is greater than 100")
Else Statements
You can also have a backup else code block that will run if the condition is not true.
number3 = 10 + 75.0
if number3 > 100:
print("number is greater than 100")
else:
print("number is not greater than 100")
Elif statements
elif statements can be used to check multiple conditions in a sequential manner. The code block under the elif statement will only run if the preceding conditions are not true.
number3 = 10 + 75.0
if number3 > 100:
print("number is greater than 100")
elif number3 > 75:
print("number is greater than 75 but not greater than 100")
else:
print("number is not greater than 75")
For loops
For loops in python are used to step through the items in a list one by
list3 = ["yellow", "orange", "red", "green", "blue"]
for value in list3:
print("Next value is: ", value)
Next value is: yellow
Next value is: orange
Next value is: red
Next value is: green
Next value is: blue
In range
You can also iterate through a set of numbers with python's range function.
Iterating Through Dictionaries
Print out each month name from your month dictionary.**
months = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December"
}
for month in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]:
print(months[month])
Working with Strings
In Python, strings are used to represent textual data. They are defined by enclosing the text in either single quotes (') or double quotes (").
String Concatenation
You can concatenate strings using the + operator:
The value of full_greeting will be
String Formatting
Python provides several ways to format strings. One common approach is to use f-strings (formatted string literals):
The value of message will be
Iterating Over Strings
We can have a for loop over strings to get individual letters that are vowels.
str1 = "A sample string to get started"
vowels = ["a", "e", "i", "o", "u"]
for letter in str1:
if letter in vowels:
print(letter)
Here would be the output:
String Operations in Python
Split
Splits a string up into a list of strings based on a separator.
Join
Joins a string back together from a list.
Replace
Replaces some part of a string.
original_str = "Item 1 | Item 2 | Item 3"
new_str = original_str.replace("|", ",")
new_str # Output: 'Item 1 , Item 2 , Item 3'
new_str = original_str.replace("|", "")
new_str # Output: 'Item 1 Item 2 Item 3'
In
Checks if one string contains another.
original_str = "Item 1 | Item 2 | Item 3"
contains1 = "Item 2" in original_str # Output: True
contains2 = "Item 4" in original_str # Output: False
Conversions
Converts between a string and a number.
String Methods
Strings have various built-in methods for manipulating and working with them. Some commonly used methods include:
- len(): Returns the length of a string.
- lower(): Converts the string to lowercase.
- upper(): Converts the string to uppercase.
- split(): Splits the string into a list of substrings based on a delimiter.
- strip(): Removes leading and trailing whitespace from the string.
text = " Hello, World! "
length = len(text) # length is 17
lower_text = text.lower() # lower_text is " hello, world! "
upper_text = text.upper() # upper_text is " HELLO, WORLD! "
split_text = text.split(",") # split_text is [" Hello", " World! "]
stripped_text = text.strip() # stripped_text is "Hello, World!"
Functions
Functions are small snippets of code that you may want to use multiple times.