CIS-1902 Python Programming

Instructor: David Cao (davidcao@seas)
TAs: Winnie Wang (winniew1@sas)
Jack Hourigan (hojack@seas)

Agenda

  1. Administrative Things
  2. Course Overview
  3. Python Introduction
  4. Python Basics
  5. Python Setup

Admin Stuff

  • Course Website is where all course information is
  • Canvas for HW submission
  • EdStem for announcements, questions, etc
  • Waitlist

Syllabus

Why Python?

  • Simplicity and readability ✨
  • Vast developer and library support that covers virtually every aspect of computer science
  • Not that performant, but can be boosted easily by calling underlying functions or libraries that are written in performant languages, e.g. C
# Isn't Python great?
print("Hello, World!")
// Java
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}
// Go
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}
// C++
#include <stdio.h>
int main() {
   printf("Hello, World!");
   return 0;
}
// rust
fn main() {
    println!("Hello World!");
}

What is Python? 🐍

  • An object-oriented, dynamically typed, interpreted language
  • Developed by Guido van Rossum in 1991 (!)
    • A scripting language that would appeal to Unix/C hackers
    • Named after Monty Python! 🐟 🌲
  • The most popular programming language in the world, only seems to be getting more traction

python dictionary definition

How do I use Python?

There are two ways of running Python. Either we can invoke Python directly within it's interpreter, or we can write files with our Python code.

  • Interpreter is similar to bash, but speaks Python. Typically used for simple and quick tasks.
  • Files are your typical codebase, used for larger projects.

Python Interpreter

❯ python
Python 3.10.12 (main, Jan 18 2024, 12:41:08) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Python Interpreter

>>> 5 + 10
15
>>> 5 ** 2
25
>>> my_var = "Hello, World!"
>>> print(my_var)
Hello, World!
>>> 

Python as Scripts

If you've written your Python in a file, you can simply run it like so

❯ echo "print('Hello, World!')" > my_script.py
❯ python my_script.py
Hello, World!

Python is Imperative

my_object = MyClass()
my_object.do_something()

but also has functional elements

def my_func(a, b, c):
  ...

# functions themselves are objects!
print(my_func)
# prints out: <function my_func at 0x102aad990>

Python Basics

Syntax

  • No curly brackets???
  • Python uses whitespace to determine scope, so readibility is built in
    • Python doesn't care if you use 2-space tabs, 4-space tabs, or tab characters as long as you stay consistent
    • Caution: make sure your editor converts tabs to spaces or spaces to tabs, otherwise you might get some strange errors!
# my incredibly insightful comment
"""
a really long and more insightful
comment that needs multiple lines
"""
print("Hello, World!")
// Java
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}

Variables & Types

  • Variables are implicitly typed
    • Type is inferred at runtime, i.e. dynamically typed
  • Variables are inherently mutable, constants do not exist!
    • We can redefine a variable to a different type without issues, but this isn't very clean code
  • Casting is easy and typically implicit
>>> my_var = 10  # no type declaration needed!
>>> type(my_var)
int
>>> my_var = "hello!"
>>> type(my_var)
str
>>> my_var = 10
>>> str(my_var)
"10"
>>> print(my_var)  # print casts everything to a str implicitly
>>> my_var = "10"
>>> int(my_var)
10
>>> my_var = None  # None is Python's null
>>> type(my_var)
NoneType
>>> type(type(my_var))  # every type is type type :)
type

Control Flow

# note the : token to indicate a scope
if x > 15:
  print("x is larger than 15")
elif x < 15:
  print("x is less than 15")
else:
  print("x is exactly 15")

# this could also be written as
if x > 15: print("x is larger than 15")
elif x < 15: print("x is less than 15")
else: print("x is exactly 15")
# but I don't recommend it as it looks messy

# however, these are useful as one-line control statements
y = "gt" if x > 15 else "lte"

Control Flow

i = 0
# again note the : token and indentation
while i < 10:
  print(i)
  i += 1

# there is no do-while in Python, instead
while True:
  do_something()
  if condition:
    break  # exit loop
for i in range(0,10):
  print(i)

for i in range(0,10):
  if i % 2 == 0:
    # skip below code and continue loop
    continue  
  print(i)

Funtions

# functions are defined using the "def" keyword
# once again, we see our good friend :
def my_func(x):
  return x + 5

def my_func(x):
  # you can stub out functions with the pass keyword
  # my_var = my_func(x)
  # will result in my_var = None
  pass

We will revisit functions later to see the power of Python's functional attributes!