Chapter 1: Python Basic Operations

In this bundle of chapters we will begin working with python and displaying just how powerful of a tool it can be when working as a data scientist. In this chapter you will learn the basics of writing, running, and reading basic python functions.

1.1 Variable Types

One important thing to understand when working in python are the different types of variables, there are four main types of variables that python works with that we will go over right now, they are integers (ints), floats, booleans, and strings.

Integers

Any whole number such as 7, 4, or 21. You can assign values in python as integers like the following.

X = 10

Floats

Floats are any number that have a decimal point such as 5.2, 8.1, and such. You can assign floats similarly.

X = 5.2

Booleans

Booleans are any true/false statements in python. You can assign a boolean like the following:

Bacon = True

Strings

Strings are any character data type like words, I am typing in a bunch of strings right now, you can assign strings like the following, note that you will need to put any strings that you are creating inside of parentheses:

Name = "Lebron James"

Checking Types

You can also check what data types an attribute is pretty simply with the following:

type(X)      # checks what type X is and will return that X is an integer
type(Name)   # checks what type Name is and will return that Name is a string

1.2 Math Operations in Python

Much like a calculator python lets you do various different math operations, you can do all of the main operations like addition and subtraction, but we will focus on the operations you might not be aware of such as floor division, exponents, incrementation, and doubling.

a = 10 // 3    # Floor division → 3
b = 2 ** 3     # Exponentiation → 8
x += 1         # Increment by 1
x *= 2         # Double x

1.3 Collections: Lists, Tuples, Sets, Dictionaries

Lists

A list is an ordered collection of items that can change (mutable)

fruits = ['apple', 'banana', 'cherry']
print(fruits[1])   # returns banana, note the 0 indexing of lists
fruits.append('cherry')  # Adds 'cherry' to the end

Tuples

A tuple is like a list, but it can’t be changed (immutable).

coords = (10.0, 20.0)

Sets

A set is a collection of unique items with no order.

unique_nums = {1, 2, 3, 3}
print(unique_nums)  # {1, 2, 3}

Dictionaries

A dictionary stores data as key-value pairs (like a mini-database).

person = {'name': 'Alice', 'age': 25}
print(person['name'])  # Alice

1.4 Logic and Loops

Conditional statements: These let your program make decisions.

x = 7
if x > 5:
    print("Big")
elif x == 5:
    print("Equal")
else:
    print("Small")

Loops let you repeat actions.

For loop:

for fruit in fruits:
    print(fruit)

While loop:

i = 0
while i < 3:
    print(i)
    i += 1

1.5 Functions

A function is a reusable piece of code that performs a specific task.

Writing your own function:

def square(x):
    return x ** 2

print(square(4))  # Output: 16

Lambda function (quick one-line function), when you read a lambda expression think of it like this: a function that takes x and returns x times 2.

double = lambda x: x * 2
print(double(5))  # Output: 10