Python Programming I

INFO 253B: Backend Web Architecture

Kay Ashaolu

Let's talk about programming

  • At the end of the day, we have to tell these computers what to do
  • Well, until they tell themselves what to do
  • That's when we have bigger problems than we do now

But on a serious note

  • In order to build back end web systems, we'll need to learn a programming language
  • A programming language enables us to tell servers/computers/boxes what to do when desktops/other servers/mobile phones/refrigerators ask a question

What is a programming language?

A programming language is a formal language, which comprises a set of instructions used to produce various kinds of output. Programming languages are used in computer programming to create programs that implement specific algorithms.

There's a few general types of programming languages

  • Compiled Languages: Languages that goes through a special program called a compiler that creates lower level code that computers directly understand
  • Interpreted Languages: Languages that are directly executed by an interpreter as the code is read line by line

Python! A interpreted Language

  • We will use Python in this class
  • Python is an interpreted language
  • In general, interpreted languages are easier to get started  

Let's learn some Python

def hello_world():
    print("Hello World!")

if __name__ == "__main__":
    hello_world()

Variables and Types

def integers_demo():
    testInt = 5
    testFloat = 2.9
    return testInt * testFloat

def string_demo():
    testString = "hello "
    testNumTimes = 3
    return testString * testNumTimes

if __name__ == "__main__":
    print(integers_demo())
    print(string_demo())

Basic String Operations

def string_demo_length():
    myString = "This is a new string"
    return len(myString)

def string_demo_substring():
    myString = "This is a new string"
    return myString[3:7]

def string_demo_reverse():
    myString = "This is a new string"
    return myString[::-1]

if __name__ == "__main__":
    print(string_demo_length())
    print(string_demo_substring())
    print(string_demo_reverse())

String Formatting

def string_demo_formatting():
    numTimes = 3
    temperature = 50.7
    message = "she sells seashells by the seashore"

    return f"I would like to write {message} {numTimes} times. By the way the temperature outside is {temperature} degrees outside."

if __name__ == "__main__":
    print(string_demo_formatting())

Lists

def testLists():
    mylist = []
    mylist.append(1)
    mylist.append(2)
    mylist.append(3)
    print(mylist[0]) # prints 1
    print(mylist[1]) # prints 2
    print(mylist[2]) # prints 3

    for x in mylist:
        print(x)

if __name__ == "__main__":
    testLists()

Dictionaries

def testDictionaries():
    phonebook = {
       "John" : 938477566,
       "Jack" : 938377264,
       "Jill" : 947662781
    }
    del phonebook["John"]
    return phonebook

if __name__ == "__main__":
    print(testDictionaries())

Questions?

Python Programming I - Backend Webarch

By kayashaolu

Python Programming I - Backend Webarch

Course Website: https://www.ischool.berkeley.edu/courses/info/253b

  • 743