Python 101

Week 1:
Introduction to Python

Expedia Code Academy

What is our mission?


Simply put, to get you coding!

By the end of this course, you'll be able to:
  • Understand how code works
  • Write basic Python code
  • Design larger programs that combine various programming concepts learned

Class Resources

There are a number of resources that we use in this class:

  • The lesson slides are found here
  • Exercises and solutions are found here
  • Slack room for the class

Why Python?

It's used by a lot of companies

It has many use cases and it's fun to program in!!

  • Process a file
  • Build a website
  • Create a game
  • Or anything else you can think of, really!

Installation


Please check out the guide!

Variables

Variables are containers for your data

The name of the variable is chosen by the software developer (that’s you!)

my_name = 'Monty'

my_age = 100

Variables


Go to pythontutor.com to test out this code:


              my_name = 'Monty'
              my_age = 100
            

Try the Visualize Execution mode to step through your program.

Variables

We can also change what is inside an existing variable:


              my_name = 'Montgomery'
              print(my_name)
            
What do you think will be printed?

Will the following lines of code work? Why/why not?


              my_Name = 'John Cleese'
              my age = 999
            

Numbers

Python can do basic maths with pretty big (or small) numbers!

You can do the usual operations on these numbers:

  • Addition e.g. 38 + 138
  • Subtraction e.g. 500 - 2222
  • Multiplication e.g. 33 * 55
  • Division e.g. 400 / 3

(⭐) 1a. Magic number trick

  • Create a variable called start_number, and make it a random number
  • Create a variable called step_1, and make it start_number multiplied by 2
  • Create a variable called step_2, and add 9 to step_1
  • Create a variable called step_3, and subtract 3 from step_2
  • Create a variable called step_4, and divide step_3 by 2
  • Create a variable called result, and subtract start_number from step_4
  • Print result

How does Python work?


              print(calculation)  # This will produce an error!
              calculation = 55 + 55
              print(calculation)  # prints 110

              calculation = 55 * 55
              print(calculation)  # prints 3025
            
  • Python evaluates files from top to bottom
  • Variable values are updated as it goes
  • print is a function that prints whatever value you give it in brackets

Strings

Strings are bits of text with apostrophes e.g.
'I am a String, with 34 characters!'

They can include letters, numbers and symbols - all of these are treated as text only, with no special value!

What do you think this line of code will do?


              mystery = '1' + '2'
              print(mystery)
            

Strings

Adding Strings together is called concatenation

It can be really useful!

            first = 'Harry'
            last = 'Potter'
            sentence = 'Welcome to Hogwarts, ' + first + ' ' + last + '!'
            print(sentence)
          

How would we concatenate (add together) the first and last to make a variable called full_name?

Accepting user input

What if we could provide data while our code is running?

The input function pauses the program until the user types something and presses Enter.

We can then use what the user typed as a variable in the code!


              users_name = input('What is your name? ')
              print('Hello, ' + users_name + '!')
            
(⭐⭐️) 1b. Ask the user to enter their first and last names, then welcome them to Hogwarts!

Appendix: More about print


You can supply multiple arguments to the print function and it will concatenate them together with a space between each one.


            first = 'Harry'
            last = 'Potter'
            greeting = 'Welcome to Hogwarts,'
            print(greeting, first, last)