Hello, World!
A Short and Incomplete Introduction
January 29, 2024
Whether you’re new to Python, or need a quick refresher:
In these slides, we’ll draw heavily from the examples in this book, as well as the structure of the coverage.
That’s pretty much it!
As is tradition…
What’s going on here?
An expression is a combination of values, variables, and operators.
Expressions are evaluated by the interpreter.
Hot Take: Just use parenthesis and make your intention clear!
1
is the argument to math.exp()
math.exp(1)
is evaluated, it is the argument to math.log()
.x
, y
, and z
are parameters.
z
has a default value of 10.s
is returned by the function.Booleans can be true and false, both of which are defined in Python as keywords with type bool
.
There are several relational operators. These compare to objects and return a boolean.
Relational operators make comparisons and return a boolean. Logical operators act on one or more booleans.
and
or
not
Python of course has the ability to use a if-else structure.
while
break
for
In Python, you can loop over all sorts of things!
Strings are sequences of characters. They are a sequence thus they are ordered and can be indexed.
Lists are are ordered, mutable, heterogeneous collections of elements.
[1, 2.0, 'Hello!', [True, False], 'a', 'b', 'c']
Dictionaries are are ordered, mutable, heterogeneous collections of key-value pairs, with some restrictions on the keys.
in
operator takes about the same amount of time no matter how many items are in the dictionary.# Changing a value
person["age"] = 31
# Adding a new key-value pair
person["profession"] = "Software Developer"
# Removing a key-value pair
del person["city"]
# Iterating over a dictionary
for key, value in person.items():
print(f"{key}: {value}")
# Checking if a key exists
if "name" in person:
print("Name is present in the dictionary")
name: John Doe
age: 31
profession: Software Developer
Name is present in the dictionary
Tuples are are ordered, immutable, heterogeneous collections of elements.
It is very common to return objects from a function using a tuple. It is equally common to “unpack” them and give each item a name.
Use with
for auto-magic, like automatic file closing.
Use with
for auto-magic, like automatic file closing.
class Circle:
def __init__(self, radius, color):
self.radius = radius
self.color = color
def display_details(self):
print(f"The circle has a radius of {self.radius} and color {self.color}")
def change_radius(self, new_radius):
self.radius = new_radius
print(f"The circle's radius has been changed to: {self.radius}")
def change_color(self, new_color):
self.color = new_color
print(f"The circle's color has been changed to: {self.color}")
# Create a Circle object with radius 5 and color red
circle = Circle(5, "red")
# Display the circle's details
circle.display_details()
# Change the circle's radius to 10
circle.change_radius(10)
# Change the circle's color to blue
circle.change_color("blue")
# Display the circle's details again
circle.display_details()
The circle has a radius of 5 and color red
The circle's radius has been changed to: 10
The circle's color has been changed to: blue
The circle has a radius of 10 and color blue
any
and all
(1, 2.0) {'third': '3'}