BYU logo Computer Science

Introduction to Python

What exactly is programming?

➡️ Telling the computer what you want it to do

Kind of like a recipe!

recipe

Or like knitting

knitting

If you teach someone to cook or to knit, they need to follow precise directions to get it right.

With programming, you need to give precise directions to the computer.

Why is programming joyful?

For the same reason that cooking or knitting is joyful — you get to make stuff!

Whatever you want! And you’re not constrained by reality!*

* there are some theoretical limitations

So let’s get started!

We’ll begin with the print statement.

print("Hello World")

Let’s use the atom code editor to put this in a file called hello.py. Then we can run it:

output of hello.py

⚠️ We will teach you how to do this in lab!

Hello World is the simplest program

  • usually the first thing everyone writes
  • the print statement does what it says
    • you give it a string, in double quotes
    • it prints the output to the screen
  • this is not the same as a mathematical function

  • in a programming langauge, a function does something for you

    • in this case, it takes a string, and prints it to the screen
  • a function is one kind of abstraction

    • you don’t have to know anything about how print actually prints to the screen
    • that’s actually kind of complicated!
  • we will talk about abstraction a lot this semester — it is a key concept in computer science

  • can you guess what the next example does?

What will this do?

Try this out by putting it into our hello.py file:

print("Hello")
print("World")

Then run python hello.py again.

What if you tell the computer the wrong thing?

Try this out by putting it into our hello.py file:

print("Hello"

Then run python hello.py again.

errors!

  File "/Users/zappala/Documents/CS110/hello.py", line 2

    ^
SyntaxError: unexpected EOF while parsing
  • this doesn’t mean you’re not smart, it just means the computer is finicky and you need to be explicit
  • sort of like forgetting to add salt to a recipe
  • if you mess up a recipe, does this mean you don’t have the gene for cooking? is there even such a thing?
  • over time you will learn how to interpret error messages and fix the error

Programs need data from you (or some other source)

  • print produces output

  • we also need input, or a way to give the program something to work on

  • in Python, we can use the input() statement to get information from a person and give it to the program

name = input("What is your name? ")
print(f"Hello, {name}, it's nice to meet you")
feeling = input("How are you feeling? ")
print(f"You're feeling {feeling}? Tell me more about that.")

Let’s explain input!

  • input takes a string and also prints it out, but then pauses and waits for you to type a response
  • it stores what you type in a variable

variables explanation

  • variables have a name
  • variables store a value
  • variables have a type (in this case, the variable is storing a string, as opposed to an integer)

Let’s explain string interpolation!

  • notice there is a letter f in front of the string for print
  • this tells Python there may be a variable inside the string
  • we surround variables in a string with curly brackets, {name}
  • Python substitutes the value of the variable (whatever you typed) and includes it in the string

string interpolation explanation

What happens with this code?

noun1 = input("Type a noun")
noun2 = input("Type another noun")
verb = input("Type a verb")
adverb = input("Type an adverb")

print(f"The {noun1} {verb} into the {noun2} {adverb}")

Things to notice

  • you can input as many things as you want
  • They must each be stored in their own variable, with a unique name

What happens with this code?

noun = input("Type a noun")
noun = input("Type another noun")
verb = input("Type a verb")
adverb = input("Type an adverb")

print(f"The {noun} {verb} into the {noun} {adverb}")
  • if you reuse the same name, it replaces whatever was there

This is known as a logic error

The code works, it just doesn’t do what you expect

Things we learned today

  • We write Python code in one or more files
  • We use Python to run the code
  • We use the print function to write output to the screen
  • We use the input function to read input from the keyboard
  • We can store the input in a variable
  • We can use stirng interpolation to place the value of a variable inside a string

The importance of abstraction

  • Abstraction is a way of dealing with complexity.
  • Build large things out of smaller things
  • Avoid having to know all the details of the smaller things
  • For example, abstraction lets you call a function without knowing how it was built or what makes it work. You just need to know what to give the function (e.g. a string to print) and then it will do the work for you.