Intro to Python

Kelsey Chan and Kelsey Wong

Intro to Python

Brought to you by Kelsey Chan (she/her) and Kelsey Wong (she/her)

Hands on the orange typewriter in a park

Outline

  1. Programming basics in Python
  2. Kahoot review activity
  3. Guess the Word project

Programming basics in Python

  1. What is a program?
  2. Variables, Expressions, and Statements
  3. Conditionals
  4. Functions
  5. Lists, Sets, and Dictionaries
  6. Loops
  7. Strings

What is a program?

Rules of Python 3

Syntax (Grammar)

Your program will only run if it follows all the rules of Python syntax.

For example, Python uses indentation to specify blocks of code.

Correct syntax
            if 10 > 0:
                print('Dog!')
            else:
                print('Cat!')
          
Wrong syntax
            if 10 > 0:
            print('Dog!')
            else:
                    print('Cat!')
          

Reserved Words (Vocabulary)

Reserved words have special meaning in Python. Later, we'll teach you how to add your own words to Python's vocabulary.

        and       del       global      not       with
        as        elif      if          or        yield
        assert    else      import      pass     
        break     except    in          raise
        class     finally   is          return
        continue  for       lambda      try
        def       from      nonlocal    while
      

Three ways to run your code

  1. Save your code in a file
  2. Use the interactive console (for code that will be used only once)
  3. Visualize your code using Python Tutor

File and interactive console

You can download Python onto your computer, but we'll be using Repl.it to run our code online.

Save your code in a file

File and interactive console

You can download Python onto your computer, but we'll be using Repl.it to run our code online.

Use the interactive console for one-time use code

Visualize your code using Python Tutor

We'll be giving you links to view code in Python Tutor, which is a tool to visualize what each line of code does.

Variables

Variables are containers for storing values. Simple examples of values are:

View Example on Python Tutor

Input Function

Python's input function allows you to collect input from the user.

Expressions

An expression is a combination of:

that evaluate to a single value.

Examples of Expressions

        4 * 5  # evaluates to 20
        "tri" + "angle"  # evaluates to "triangle"
        my_variable + 1
      

Statements

A statement is like an instruction. We've already seen some:

        print("Hello world!")
        new_variable = 10
      
In upcoming slides, we'll learn about more types of statements.

Boolean Expressions

A boolean expression is a specific type of expression that evaluates to a boolean value - either true or false.
View Example on Python Tutor

Logical Operators

The logical operators and, or, and not allow us to write more complicated boolean expressions.
View Example on Python Tutor

If/Else Statements

If/else statements let you tell the program how to make decisions. Every if/else statement has a boolean expression as its condition.

        if number_of_points >= 10:
            print("You win!")
        else:
            print("You still need more points")
      

Functions

A function is a group of statements that together do a particular task. Functions help to organize, reuse, and clarify our code.

Syntax of a Function

          def double(number):
              doubled = number * 2
              return doubled
        

Calling a Function

After you define a function, you can use it by calling it, which requires the function name with the correct number of parameters.
View Example on Python Tutor

Function Return Statement

Functions can optionally include return statements, with 0 or more expressions to return.
View Example on Python Tutor

Lists, Sets, and Dictionaries

Data structures are used to organize and store data.

Lists

Used to store an ordered sequence of values

View Example on Python Tutor

Sets

Like lists, but they do not store duplicate entries, and they are unordered

View Example on Python Tutor

Dictionaries

Used to store values associated with keys

View Example on Python Tutor

Loops

For loops

The for loop is useful for iterating over a sequence.

View example on Python Tutor

While loops

The while loop is used to repeatedly execute instructions as long as a boolean condition is met.

View example on Python Tutor

Break and continue

View example on Python Tutor

Strings

Strings are sequences of letters surrounded by double or single quotes.

View example on Python Tutor

Outline

  1. Programming basics in Python
  2. Kahoot review activity
  3. Guess the Word project

Link to Kahoot

Outline

  1. Programming basics in Python
  2. Kahoot review activity
  3. Guess the Word project

Visit the link below to get started on the Guess the Word project.

coda.io/@kelsey/intro-to-python

Remember you can ask questions in the chat!