Python Basics I
January 31, 2025
A value is datum (literal) such as a number or text.
There are different types of values:
Sometimes you will hear variables referred to as objects.
Everything that is not a literal value, such as 10
, is an object.
data.frame
data.frame
is a table-like data structure used for storing data in a tabular format with rows and columns.data.frame
.=
)# 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.
#
mark is Google Colab’s comment character.
#
character has many names: hash
, sharp
, pound
, or octothorpe
.#
indicates that the rest of the line is to be ignored.y = x + 12
, it does the following:
=
in the middle.x
and adds it to 12
).y
.10
1.23
"like this"
True
None
[10, 15, 20]
) that can contain anything, even different 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.
[]
, {}
, and ()
.[]
is used to denote a list or to signify accessing a position using an index.{}
is used to denote a set or a dictionary (with key-value pairs).string_one = "This is an example "
string_two = "of string concatenation"
string_full = string_one + string_two
print(string_full)
+
for addition-
for subtraction*
for multiplication**
for powers/
for division//
for integer divisionUsing Python operations only, calculate below: \[\frac{2^5}{7 \cdot (4 - 2^3)}\]
Sometimes we need to explicitly cast a value from one type to another.
str()
, int()
, and float()
.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.
In everyday programming, we use lists and dictionaries more than tuples.
cities_to_temps = {"Paris": 28, "London": 22, "New York": 36, "Seoul": 29}
cities_to_temps.keys()
cities_to_temps.values()
cities_to_temps.items()
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?