Python Basics

Display text

print("Hi there")
  • This will display the text to the console.
  • Alternatively you can replace each double quote symbol with a single quote.

Add comments

# My comment
  • Anything you type to the right of "#" is a comment and will not be read as code.
  • You can have a comment block (multiple lines) by putting code between two sets of 3 quote symbols ''' single quote ''' , or """ double quotes """.

Store values (variables)

x = 5
  • You can then access this value later by name.
  • A variable name can be more than 1 letter, and names with multiple words are usually separated with an underscore (e.g. 'my_value').
  • You can also store text (e.g. my_name = "Bob").

Do math

365 / 7
  • You can also use +, -, *, ...

Display multiple things

print("I am", x, "years old")
  • This will display "I am 5 years old", assuming you ran the code to give x a value.

All together

name = "Jane" # Set name age = 22 # Set age print(name, "is at least", age * 12, "months old" )
  • This will display "Jane is at least 264 months old".

Challenge

Write a program that will calculate and display how many $16 items Bob can buy with his savings. Store the savings as a variable value, first set it to $736, then $1232.

Completed