Programming in Python

BEES 2023

What we will cover

  • An introduction to programming in Python

  • An introduction to theoretical problems in computer science

Sample Code

# Hello world program - this is a comment 
# (it's just to make the code readable)

print("Hello, world!") 

A bigger example

## A basic program

# Inputs - let's ask the user to input two numbers and 
# call them x and y (x and y are called variables)
x = float(input("Please enter a number: "))
y = float(input("Please enter another number: "))

# Doing stuff

z = x + y # Add the inputs together

# Outputs
# Let's print the result
print("The sum of your two inputs is:", z)

Programming Notebooks

Other ways to run python

  • Interactively via interpreter
  • Via a Dev environment (Pycharm demo)

comments

# This is a comment

# Code can be hard to read, and yet you 
# often have to edit and maintain code over time.

# Comments let you keep notes in your code.

# Rule of thumb: Good code is often about 50% code and 50% comments

# It is amazing to read code you wrote last year - you
# likely won't remember how it works without comments!

# 80 Character limit: Because some text editors don't automatically line-wrap it is a good idea to try to keep your comments to less than 80 or so characters by spreading them across multiple lines. 

# Later on, we'll see comment strings, which are another way
# of documenting code.

Basic Types

  • In general, a type is the formatting of bits to represent a given information

  • Types impose constraints, e.g. ints are whole numbers only

Integer

Float

String

Boolean

Basic Types

# What's the "type" of 1?

# For now, just understand that we can use type() 
# to find out the 'type' of any object in Python

type(1)
# Outputs <class, 'int'> or prints int

# Similarly, 5 is an integer (called an 'int')
type(5)

# Okay, so what's the type of a decimal?
type(3.142)

# And the type of 1.0? 
# (there is no room for indecision/ambiguity in a programming language)
type(1.0)

# What about text?
type("hello")

String type

# Strings can be expressed a bunch of other ways:

type("hello") # Double quotes

type('hello') # Single quotes

type("""hello""") # Triple double quotes

type('''hello''') # Triple single quotes

# We can embed single quotes in double quotes:
type("This is a string containing a 'single quoted substring'")

# Trying to embed single quotes 
# in a single quoted string naively does not work
type('This is a string containing a 'single quoted substring'')

# And vice-versa, we can embed double quotes in single quotes:
type('This is a string containing a "double quoted substring"')

# Single and double quoted strings can't span multiple lines. This is an error
type('This is an 
      attempt to have a string on two lines')

Triple quotes

# Triple quotes (either single or double), allow embedding essentially anything, 
# including spanning multiple lines:

type("""Boom, now
   we can make crazy strings that ' nest quotes in quotes " " ' etc.
   and which span multiple lines""")
     
# You will also see this a lot in Python code:

""" 
A comment string. 

These can be used to document some code
across multiple lines. We'll later see this at the top
of classes and functions as a standard way of giving
a description string.
"""

# Will print: " \nA comment string. \n\nThese can be used to document some code\n ... "

Type Conversion

# It is useful to know that int, float and string types can be cross converted

type( int('5') ) # This int() turns the string into an integer 

type( str(5) ) # We can also go back the other way

# Of course, this doesn't work because int() doesn't how to interpret 'foo'
int("foo")

# We can also convert between floats and ints, doing 
# rounding as we go float --> int
int(5.999)

float(5)
# Prints 5.0

putting it all together

Lecture 1 challenge

Questions?