Skip to content

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:

98.7

Other times it will be a text string such as a name:

"New York City"

More advanced cases will have lists of elements such as many names:

["Queens", "Brooklyn", "Manhattan", "Staten Island", "The Bronx"]

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.

my_number_variable = 80

Addition:

You can add two numbers together to output number3:

number1 = 10.5
number2 = 20
number3 = number1 + number2
print(number3) # 30.5

Strings

Strings are very easy to use in python. You can just use quotes to create them.

string1 = "New York "
string2 = "City"

Concatenation:

To combine two strings you simply add them together.

string3 = string1 + string2
print(string3) # "New York City"

Lists

Python has a simple type for multiple values called a list.

list1 = [1, 2, 3]
list2 = [4, 5]

Adding Two Lists:

Adding two lists together creates a new list combining the two.

list3 = list1 + list2
print(list3) # [1, 2, 3, 4, 5]

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.

dict1 = {"apple": "red",
         "banana": "yellow"}

To access a value of the dictionary, you use the square bracket notation with the key that you want to access.

dict1["apple"] # "red"
dict1["banana"] # "yellow"

Adding Values to a Dictionary

You can also add a new key to the dictionary by setting its value.

dict1["pear"] = "green"
print(dict) # dict1 = {"apple": "red", "banana": "yellow", "pear": "green"}

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)
Here is the correspoding output:
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.

for val in range(10):
    print(val)
Here is the corresponding output:
0
1
2
3
4
5
6
7
8
9

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])
Here is the corresponding output:
January
February
March
April
May
June
July
August
September
October
November
December

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 (").

name = "John Doe"
message = 'Hello, World!'

String Concatenation

You can concatenate strings using the + operator:

greeting = "Hello"
name = "Alice"
full_greeting = greeting + " " + name

The value of full_greeting will be

"Hello Alice"

String Formatting

Python provides several ways to format strings. One common approach is to use f-strings (formatted string literals):

name = "Bob"
age = 25
message = f"My name is {name} and I'm {age} years old."

The value of message will be

"My name is Bob and I'm 25 years old."

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:

A
a
e
i
e
o
e
a

String Operations in Python

Split

Splits a string up into a list of strings based on a separator.

str1 = "a:b:c"
list_of_splits = str1.split(":")
list_of_splits[1]
# Output: 'b'

Join

Joins a string back together from a list.

str1 = ",".join(list_of_splits)
str1 # Output: 'a,b,c'

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.

int1 = int("15")
int1
# Output: 15
decimal1 = float("15.50")
decimal1 # Output: 15.5

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.

def add_man(str1):
    return str1 + "man"

y = "bat"
out = add_man(y)
out # Output: 'batman'