Lecture 4

Python Basics I

Byeong-Hak Choe

SUNY Geneseo

January 31, 2025

Python Basics

Python Basics

Variables Are Names, Not Places

  • A value is datum (literal) such as a number or text.

  • There are different types of values:

    • 352.3 is known as a float or double;
    • 22 is an integer;
    • “Hello World!” is a string.

Python Basics

Values, Variables, and Types

a = 10
print(a)

  • A variable is a name that refers to a value.
    • We can think of a variable as a box that has a value, or multiple values, packed inside it.
  • A variable is just a name!

Python Basics

Values, Variables, and Types

  • Sometimes you will hear variables referred to as objects.

  • Everything that is not a literal value, such as 10, is an object.

Python Basics

Variable in data.frame

  • Definition: A data.frame is a table-like data structure used for storing data in a tabular format with rows and columns.
  • Structure: Consists of:
    • Variables (Columns)
    • Observations (Rows)
    • Values (Cells): Individual data points within each cell of the data.frame.

Python Basics

Assignment ( = )

# Here we assign the integer value 5 to the variable x.
x = 5   

# Now we can use the variable x in the next line.
y = x + 12  
y
  • In Python, we use = to assign a value to a variable.

  • In math, = means equality of both sides.

  • In programs, = means assignment: assign the value on the right side to the variable on the left side.

Python Basics

Code and comment style

  • The two main principles for coding and managing data are:
    • Make things easier for your future self.
    • Don’t trust your future self.
  • The # mark is Google Colab’s comment character.
    • The # character has many names: hash, sharp, pound, or octothorpe.
    • # indicates that the rest of the line is to be ignored.
    • Write comments before the line that you want the comment to apply to.
  • Consider adding more comments on code cells and their results using text cells.

Python Basics

Assignment

  • In programming code, everything on the right side needs to have a value.
    • The right side can be a literal value, or a variable that has already been assigned a value, or a combination.
  • When Python reads y = x + 12, it does the following:
    1. Sees the = in the middle.
    2. Knows that this is an assignment.
    3. Calculates the right side (gets the value of the object referred to by x and adds it to 12).
    4. Assigns the result to the left-side variable, y.

Python Basics

Variables Are Names, Not Places

list_example = [10, 1.23, "like this", True, None]
print(list_example)
type(list_example)
  • The most basic built-in data types that we’ll need to know about are:
    • integers 10
    • floats 1.23
    • strings "like this"
    • booleans True
    • nothing None
  • Python also has a built-in type of data container called a list (e.g., [10, 15, 20]) that can contain anything, even different types

Python Basics

Types

  • The second column (Type) contains the Python name of that type.

  • The third column (Mutable?) indicates whether the value can be changed after creation.

Python Basics

Brackets

  • There are several kinds of brackets in Python, including [], {}, and ().
vector = ['a', 'b']
vector[0]
  • [] is used to denote a list or to signify accessing a position using an index.
{'a', 'b'}  # set
{'first_letter': 'a', 'second_letter': 'b'}  # dictionary
  • {} is used to denote a set or a dictionary (with key-value pairs).
num_tup = (1, 2, 3)
sum(num_tup)
  • () is used to denote
    • a tuple, or
    • the arguments to a function, e.g., function(x) where x is the input passed to the function.

Python Basics

Operators

string_one = "This is an example "
string_two = "of string concatenation"
string_full = string_one + string_two
print(string_full)
  • All of the basic operators we see in mathematics are available to use:
  • + for addition
  • - for subtraction
  • * for multiplication
  • ** for powers
  • / for division
  • // for integer division
  • These work as you’d expect on numbers.
  • These operators are sometimes defined for other built-in data types too.
    • We can ‘sum’ strings (which really concatenates them).

Python Basics

Operators

list_one = ["apples", "oranges"]
list_two = ["pears", "satsumas"]
list_full = list_one + list_two
print(list_full)
  • It works for lists too:
string = "apples, "
print(string * 3)
  • We can multiply strings!

Python Basics

Operators

Q. Classwork 4.1

Using Python operations only, calculate below: \[\frac{2^5}{7 \cdot (4 - 2^3)}\]

Python Basics

Casting Variables

orig_number = 4.39898498
type(orig_number)
mod_number = int(orig_number)
mod_number
type(mod_number)
  • Sometimes we need to explicitly cast a value from one type to another.

    • We can do this using built-in functions like str(), int(), and float().
    • If we try these, Python will do its best to interpret the input and convert it to the output type we’d like and, if they can’t, the code will throw a great big error.

Python Basics

Tuples and (im)mutability

  • A tuple is an object that is defined by parentheses and entries that are separated by commas, for example (15, 20, 32). (They are of type tuple.)

  • Tuples are immutable, while lists are mutable.

  • Immutable objects, such as tuples and strings, can’t have their elements changed, appended, extended, or removed.

    • Mutable objects, such as lists, can do all of these things.
  • In everyday programming, we use lists and dictionaries more than tuples.

Python Basics

Dictionaries

cities_to_temps = {"Paris": 28, "London": 22, "New York": 36, "Seoul": 29}

cities_to_temps.keys()
cities_to_temps.values()
cities_to_temps.items()
  • Another built-in Python type that is enormously useful is the dictionary.
    • This provides a mapping one set of variables to another (either one-to-one or many-to-one).
    • If you need to create associations between objects, use a dictionary.
  • We can obtain keys, values, or key-value paris from dictionaries.

Python Basics

Running on Empty

  • Being able to create empty containers is sometimes useful, especially when using loops.

  • The commands to create empty lists, tuples, dictionaries, and sets are lst = [], tup=(), dic={}, and st = set() respectively.

  • Q. What is the type of an empty list?

Google Colab Settings

Turn off AI Assistance

  • On Google Colab
    1. From the top-right corner, click ⚙️
    2. Click “AI Assistance” from the side menu.
    3. Disable all options.