Skip to the content.

The Very Basics

🔗 Comments in Python

Comments give programmers a way to annotate their code.

# The '#' symbol denotes a single-line comment. For example,
# This is a single-line comment!

"""
Three double quotes can be used to denote the start and end of a... you guessed it...
a multi-line comment!



This is all part of this multi-line comment!

And this, too!
"""

''' You can also use single quotes to denote a multi-line comment.

Remember, though, a multi-line comment started with three single-quotes must be
ended with three single-quotes!

For example, """
does not end this multi-line comment. But, '''

Why use comments?

  • To document your programs!
    • (1) For future you to understand what past you was thinking
    • (2) For other programmers who might read or use your code

🔗 Printing to the screen

We can use the print instruction to print things to the screen:

print("Hello world")
print("My name is Josh Nahum")
print("I'm ??? years old.")
print("Ikea is adult disney land.")

Note that every time you run your program, those sentences are in the same order. Python programs are executed procedurally, instruction-by-instruction, from top to bottom.

🔗 What is a variable?

We use variables to store data and subsequently refer to that data. As a pet name lets us to refer to our 🐶, variables give us a way to refer to our data.

Variables in python must begin with a letter or underscore, but after the first character letters, numbers, and underscores are 👌. In contrast to pet names (where your pet does not care about the capitalization of their name), python variables are case sensitive (i.e, ‘DOGGO’ is not the same as ‘doggo’).

We can assign ‘strings’ to variables:

first_name = "Josh"     # This is a string
last_name = "Nahum"  # This is also a string

In this above example, first_name is a variable. I assigned it the value "Josh" using the assignment operator =.

We can also assign numbers to variables:

age = 65              # This is an integer
half_of_three = 1.5   # This is a float (i.e., it has a decimal)

We can refer to previously assigned variables. For example, we can print out the values of some of our variables:

print(first_name)
print(last_name)
print(age)

🔗 Python can be used as a glorified calculator

You can (and we will) use python as a fancy calculator. All the classics are available out of the box: addition +, subtraction -, multiplication *, division /, modulus % (remainder), and exponentiation **.

In python:

this_year = 2021
my_age = 65

twice_my_age = 2 * age

ten_years_from_now = this_year + 10

year_born = this_year - my_age

half_my_age = age / 2

Just like in math class, you can enforce a particular order of operations using parentheses:

a = (1 + 1) * (2 + 2) # = 8
# is different from
b = 1 + 1 * 2 + 2     # = 5

More math is available in Python’s math module.

🔗 Getting user input

We can use python’s input() instruction to request information from the user:

user_input = input("I demand input: ")

In the code above, whatever the user types in gets stored in user_input. The string "I demand input: " is used as the prompt for input.

🔗 Working with strings

Find more things you can do to strings here: https://docs.python.org/3/library/stdtypes.html#string-methods

🔗 Basics - Challenges

  • (1) Write a program that take a user’s name and age in as input, and prints the year they were born.
  • (2) Adjust program so that user’s name is printed in ALL CAPS (hint: checkout Python’s string documentation)
  • (3) What are some special characters that aren’t obvious in how we can include them in a python string (e.g., so we can print them)? Think about things you use all the time when writing an essay or an email. E.g., what might you start a paragraph with?

🔗 Basics - Example code

We’ll upload our example code after the lesson! :^)