Redmond Python Meetup Onboarding
Overview
Welcome!
If you are reading this page, you have expressed an interest in attending the Redmond Python Meetup. The Redmond Python Meetup is for everyone–from new programmers to experienced developers.
This page will walk you through the following:
- How to install Python on your computer
- How to use the Python Interpreter to run Python code
- How to write and manipulate basic Python data types, functions, and loops
At this point, you probably fall into one of three categories based on your experience level:
Experience Level | Recommendation |
---|---|
“I’m brand new to development and Python.” | Come to the meetup for a guided on-boarding session with our TAs where you can complete this page and ask questions as you go. If you’re feeling adventurous, you can start with installation below and try to get as far as you can. |
“I’m new to Python but I have some programming experience.” | Feel free to get as far as you can on this page and come to the meetup with questions, or you can come to the meetup and do an on-boarding session with our TAs. |
“I’m an experience developer” | Feel free to go through this page on your own and come to the meetup with any questions |
About Python
Python is an easy-to-write, easy-to-read interpreted scripting language. “Hello world!” is one line and looks like this:
1 | "Hello world!") print( |
Here is a more complex example application that sends an email. It’s still only 5 lines, though.
1 | import smtplib |
The above code does the following:
- Imports some code in an external module (code that we didn’t write, but that are nevertheless using).
- Creates an SMTP object to connect to a server.
- Logs in to the server.
- Creates our message.
- Sends the message.
You don’t need to understand this syntax at this time. The purpose of this example is simply to show you what you can accomplish in a few lines of Python code, which may take dozens of lines in other programming languages.
Basic Python Features
There are a few characteristics of Python that you should remember:
- Python is open source: Anyone can see all of the underlying code and everyone has the chance to contribute to the code base.
- Python is object-oriented: Everything in Python is an object.
- Python is platform-independent: You can write an application on one operating system and run it on another.
- Python has a minimalist design philosophy: This emphasizes cleanliness and readability, with minimal semicolons and brackets.
What kinds of things can you do with Python?
The sky is the limit, but here are some examples:
- Interact with files on your computer
- Automate tasks as part of a workflow
- Scrape data off a website
- Build and deploy REST APIs
- Build games
- Interact with cloud services (AWS, Azure, Google, etc.)
Setup
Installing Python
Let’s first get Python installed on your computer. There are two major versions of Python out there in the wild right now: Python 2 and Python 3. Python 2 is retiring in 2020, so we’ll be using Python 3.
In fact, you might already have one of these versions installed on your computer. Let’s check:
- Open a terminal or command prompt.
- WIN: Start Menu > Command Prompt.
- MAC/LIN: Application > Terminal.
Type
python
and hit ENTER. Do you see something like this? It means you have Python 3 installed on your computer.1
2
3
4Python 3.6.4 (default, Jan 3 2018, 12:27:09)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>If you get an error, you probably need to install Python. Note that you may have Python 2 installed (it will say
Python 2.x.x
), in which case you’ll want to install Python 3.
See the following pages for platform-specific instructions on installing Python:
When you’re finished, come back here and continue to the next section.
Using the Python Interpreter
Now that we have Python installed, let’s open the Python interpreter. The interpreter is a small application that let’s us type some Python and execute it easily:
- Open a terminal or command prompt like you did in the previous section.
- Type
python
and hit ENTER.
Your prompt should look something like the following (if it doesn’t, let a TA know):1
2
3
4Python 3.6.4 (default, Jan 3 2018, 12:27:09)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
You’ll know you’re in the Python interpreter when you see this prompt:1
>>>
This means the Python interpreter is ready for your input. We can type Python code directly into this prompt and it will execute. Note that as you go through this on-boarding page, you’ll see code samples like this:1
>>> a = "Hello"
This means that you can type this line in at the >>>
prompt. Simply enter the line of code, hit Enter after every line you enter, and note the output (although sometimes there won’t be any!).
Tip: Don’t copy and paste commands – type them out. You’ll learn far more if you take the time to type everything yourself.
To exit the interpreter:
- Type
exit()
or hitctrl+d
to exit the Python interpreter and return to the computer’s terminal. - Now, reopen the Python interpreter. A key point here is that everything that you entered in that previous Python interpreter session is lost between sessions. The data does NOT persist.
For now, we’ll just be working with Python via the interpreter while we learn the basics. We’ll show you later how to write lines of code in a file and run a full Python script at the command line.
Getting Started with Python Code
Basic Operations
Let’s do some basic operations in Python to teach you how to interact with the Python interpreter.
- Open the Python interpreter like we showed you in the previous section.
Type the following lines, noting the output:
1
2
3
42 + 4
5 - 1
3 * 4
10 / 2Awesome! This is how easy it is to write and execute Python. This also introduces you to some basic math operations available to you in Python like addition, subtraction, etc. We’ll expand on those later.
Now try these:
1
2
3
42+4
5-1
3*4
10/2Same operations as before. Note that the whitespace between values, in this case, doesn’t matter. In Python,
2+4
is the same as2 + 4
.
Variables
Next, let’s introduce variables. Variables are used to store values–numbers, words, lists of numbers and words, etc. This allows us to reference these variables later in our application. Imagine if you were writing an application and wanted to display your company name in 20 different places. Instead of writing your company name out 20 times, you can write it once and store it in a variable, and then reference the variable wherever you need it.
Storing Numbers
Let’s create a few variables that store some simple numbers. We’ll use the =
symbol to store values. This equals sign is formally called the assignment operator.1
2
34 x =
2 y =
print(x + y)
What did we just do?
- We assigned the number 4 to the variable “x” and the number 2 to “y”.
- We then called the “print” function to print the sum.
print()
is a built-in function that we can access anywhere, and always prints the contents out to the console.
For experienced developers, note that we don’t have to tell the interpreter that our variables are numbers. We just assign and run. Python will assign the type automatically when we run our program (“at runtime”).
Storing Strings
Numbers are great, but let’s try entering some strings of text. Go ahead and type the following, replacing the value for the name
variable with your name below:1
2
3
4
5
6'Your Name' name =
print(name)
"Pasta" fav_food =
print(fav_food)
"""When in the Course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.""" long_multiline_string =
print(long_multiline_string)
Notice the use of quotes–single, double, or triple quotes are acceptable in Python. Just be consistent throughout your application! To keep things simple, we’ll use double quotes and all of our variables will start with a lowercase letter for now. We’ll be expanding on variables and functions later.
Remember: Once you close the interpreter, all of this data is lost.
Numbers
There are two number data types in Python 3:
- integers: Whole numbers with no decimal point (e.g. 1, 2, -504803, 6238746289374692837649283764829).
- floats: Written with a decimal point dividing the integer and fractional parts (e.g. .5, 842.32, .000238923).
Math in Python looks a lot like math with a calculator. Type the following into the interpreter.
Addition
1 | 2 + 2 |
Subtraction
1 | 12 - 5 |
Multiplication
1 | 6 * 7 |
Division
1 | 12/3 |
If you’ve used Python 2, you’ll see that division works differently in Python 3. Python 2 uses floor division for integers, meaning it will return only the whole number part of the answer. Python 3 performs true division, returning the real or true value of the division. Try the following, one with one /
and the other with two //
:1
216/5
16//5
Modulus
Thinking back to long division that you may have learned in school, the modulus is the “remainder” after performing division. It uses the % symbol.1
216%5
50%4
Order of Operations
Order of operations works just like you learned in math class - Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.1
25 + 4 * 3
5 + 4) * 3 (
Note: We’ve just covered a lot! Using the interpreter, variables, entering basic data types, doing some basic math….questions? Ask a TA!
Booleans
So far, the code we’ve written has been unconditional: no choice is getting made in the program–all of the code runs. Python has another data type called a boolean that is helpful when writing code that makes decisions. Booleans hold two values: True
and False
.
Open the Python interpreter and type these:1
2
3
4
5
6True
True) type(
False
False) type(
true
false
Testing for Equality
You can use booleans to test if Python objects are equal or unequal. The result is a boolean. Try typing these expressions in your Python console (note that the double equals tests for equality):
1 | 0 == 0 |
Use ==
to test for equality. Recall that =
is used for assignment of a variable to a value. This is an important idea and can be a source of bugs until you get used to it: =
is assignment, ==
is comparison.
Testing for Inequality
To test for inequality, use !=
:
1 | "a" != "a" |
The above example demonstrates an important point: Python is CASE-SENSITIVE. Uppercase and lowercase matter.
Comparison Operators
Next, let’s look at comparison operators. <
, <=
, >
, and >=
have the same meaning as in math class. The result of these tests is a boolean:
1 | 1 > 0 |
Membership Operators
Finally, let’s briefly look at membership operators. You can check for membership using the in
keyword, which also results in a boolean:1
2
3
4"H" in "Hello"
"h" in "Hello"
"He" x =
in "Hello" x
Or check for a lack of membership with not in
:1
2"a" not in "abcde"
"Chicago" not in "Redmond Python Workshop"
Operators
We just introduced some operators that let us further manipulate our data. We’ve outlined some (not all) of the Python operators in the following tables:
Assignment Operators
Operator | Description | Example |
---|---|---|
= |
Assigns values from right side operands to left side operand | c = a + b assigns value of a + b into c |
+= Add AND |
Adds right operand to the left operand and assign the result to left operand | c += a is equivalent to c = c + a |
-= Subtract AND |
Subtracts right operand from the left operand and assign the result to left operand | c -= a is equivalent to c = c - a |
*= Multiply AND |
Multiplies right operand with the left operand and assign the result to left operand | c *= a is equivalent to c = c * a |
/= Divide AND |
Divides left operand with the right operand and assign the result to left operand | c /= a is equivalent to c = c / a |
%= Modulus AND |
Calculates modulus using two operands and assign the result to left operand | c %= a is equivalent to c = c % a |
**= Exponent AND |
Performs exponential calculation on operators and assign value to the left operand | c **= a is equivalent to c = c ** a |
//= Floor Division |
Performs floor division on operators and assign value to the left operand | c //= a is equivalent to c = c // a |
Exercise:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1821 a =
10 b =
0 c =
c = a + b
c
c += a
c
c *= a
c
c /= a
c
2 c =
c %= a
c
c **= a
c
c //= a
c
Comparison Operators
Assume a = 5
and b = 10
.
Operator | Description | Example |
---|---|---|
== |
If the values of two operands are equal, then the condition becomes true. | a == b evaluates to false. |
!= |
If values of two operands are not equal, then condition becomes true. | a != b is true. |
> |
If the value of left operand is greater than the value of right operand, then condition becomes true. | a > b is not true. |
< |
If the value of left operand is less than the value of right operand, then condition becomes true. | a < b is true. |
>= |
If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | a >= b is not true. |
<= |
If the value of left operand is less than or equal to the value of right operand, then condition becomes true. | a <= b is true. |
Exercise:1
2
3
4
5
6
75 a =
10 b =
a == b
a != b
5 b =
a == b
a != b
Key Point: =
assigns a value, ==
tests for equality, !=
tests for inequality.
Logical Operators
Operator | Description | Example |
---|---|---|
and |
If both the operands are true then condition becomes true. | x>4 and y<5 |
or |
If any of the two operands are non-zero then condition becomes true. | x>4 or y<5 |
not |
Used to reverse the logical state of its operand. | not(x and y) |
Identity Operators
Operator | Description | Example |
---|---|---|
is |
Evaluates to true if the variables on either side of the operator point to the same object. | See below. |
is not |
Evaluates to false if the variables on either side of the operator point to the same object. | See below. |
Exercise:1
2
3
4
51 a =
1 b =
is b a
2 b =
is b a
Conditional Branching
Using if
Now that we know how to check if something is True
or False
using booleans and operators, we can use “conditional branching” to make Python execute commands on a conditional basis. Just take a look at the following code:
1 | if 6 > 5: |
This is the first piece of Python we’ve written that crosses multiple lines, and the way to enter it at a Python interpreter’s prompt is a little different than single lines of code.
In the interpreter:
- Type
if 6 > 5:
, and hitENTER
. The next line will have...
as a prompt, instead of the usual>>>
. This is Python telling us that we are in the middle of a code block, and so long as we indent our code it should be a part of this code block. - Type 4 spaces, type
print("Six is greater than five!")
, and then hitENTER
to end the line. Note that spaces are officially the preferred indentation method. - Finally, hit
ENTER
again to tell Python you are done with this code block. All together, it will look like this:
1 | if 6 > 5: |
So what’s going on here? When Python encounters the if
keyword, it evaluates the expression following the keyword and before the colon.
- If that expression evaluates to
True
, Python executes the code in the indented code block under theif
line. - If that expression evaluates to
False
, Python skips over the code block.
In this case, because “6 is greater than 5” evaluates to true, Python executes the code block under the if statement, and we see “Six is greater than five!” printed to the screen. Guess what will happen with these other expressions, then type them out and see if your guess was correct:
1 | if 0 > 2: |
Another:1
2if "banana" in "bananarama":
"I miss the 80s.") print(
One more:1
2if "Ringo" not in "John, Paul, and George":
"Ringo wasn't much of a songwriter.") print(
Using if
and else
You can use the else
keyword to execute code only when the if
expression isn’t True
:
1 | 15 sister_age = |
Like with if
, the code block under the else
statement must be indented so Python knows that it is a part of the else
block.
Using and
and or
We’ve been testing single conditions, but we can also test multiple conditions that result in execution of some code. You can check multiple expressions together using the and
and or
logical operators.
- If two expressions are joined by an
and
, they both have to beTrue
for the overall expression to beTrue
. - If two expressions are joined by an
or
, as long as at least one isTrue
, the overall expression isTrue
.
Try typing these out and see what you get:
1 | 1 > 0 and 1 < 2 |
Guess what will happen when you enter these next two examples, and then type them out and see if you are correct. If you have trouble with the indenting, call over a staff member and practice together. It is important to be comfortable with indenting. Indenting is a crucial part of the syntax of Python.
1 | 32 temperature = |
One more:1
2
3
4
5
6
711 hour =
if hour < 7 or hour > 23:
"Go away!") print(
"I'm sleeping!") print(
else:
"Welcome to the cheese shop!") print(
"Can I interest you in some choice gouda?") print(
You can have as many lines of code as you want in if-else block, just make sure to indent them so Python knows they are a part of the same indentation block.
Using elif
and else
If you have more than two cases, you can use the elif
keyword to check more cases. Think of elif
as Python-speak for else if. You can have as many elif
cases as you want. Python will go down the code checking each elif
until it finds a True
condition or reaches the default else
block.
1 | 15 sister_age = |
You don’t have to have an else
block if you don’t need it. That just means there isn’t default code to execute when none of the if
or elif
conditions are True
:
1 | "orange" color = |
If color
had been “purple”, the code wouldn’t have printed anything. Remember that =
is for assignment and ==
is for comparison.
Functions
Functions are how we perform actions on our data. We’ve been using a function a lot so far: print()
. Calling a function is as easy as calling the function name and putting a variable or value between round brackets like we’ve been doing.
Try the following:1
2
3
4
5"The number 3: " a =
3 b =
type(a)
type(b)
print(a, b)
The values between the ()
brackets are called “arguments.” Certain functions let us pass arguments to our functions. like so:1
2
3
4"Bill is" str1 =
50 a =
"years old." str2 =
print(str1, a, str2)
There are three basic categories of functions:
- The built-in kind that come with Python when we install it (e.g.
print()
) - The kind we write ourselves
- The kind that other people wrote that we can borrow (by importing their code modules and using them in our programs)
We’ve already introduced the built-in kind. Next, we’ll show you how to write your own. Later, we’ll show you how to import and use functions written by other people.
Writing Our Own Function
In the interpreter, write the following code:1
2def myFirstFunction():
"Hello!") print(
We just wrote our first function! Now call it:1
myFirstFunction()
Our function takes no arguments. Let’s write a function that passes an argument:1
2def mySecondFunction(name):
"Hello", name) print(
Now let’s run it, passing in the name of your choice:1
"Bill Gates") mySecondFunction(
Fantastic! You just authored and called your first function that passed an argument.
Utility Functions
We’ll gradually expand your knowledge of functions, but for now we’ll introduce a few other built-in functions that can help us with numerical types and strings.
For Numbers
We can convert between numerical types with ease.
Convert from float to int:1
2
3
4
5
65.5 x =
x
type(x)
x = int(x)
x
type(x)
Convert from int to float:1
2
3
4
5
64 y =
y
type(y)
y = float(y)
y
type(y)
For Strings
upper()
and lower()
are handy:1
2
3"My String" str =
str.upper()
str.lower()
Additional Concepts
Let’s briefly introduce some additional concepts and tools that will help you on your journey with Python.
Tracebacks
When you make a mistake and try to run your program, Python will generate a traceback to tell you what went wrong. By now, you may have seen a few of these!
Open the interpreter and run:1
2"John" a =
2 a +
We see the following error:1
2
3Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
The above traceback tells us the following:
- The name of the file where the error occurred: “
“ (means that the error occurred on input) - The line number containing the error: 1
- The name of the module (if applicable, which it’s not here)
- The error type:
TypeError
- What we did wrong: We tried to perform an operation on one data type that Python wouldn’t allow.
Tracebacks are invaluable when trying to diagnose why something won’t run.
Comments
Comments help us communicate with teammates about what our functions are doing. We’ll use them more when we start running applications in Python files. For now, just know that when you see the following syntax, it’s a comment. There’s no need to enter the code below, just familiarize yourself with the concepts.
Single line comment:1
2
3
4# Here is a comment in python! It's great and is ignored when the program runs!
name = "Your Name"
print(name)
print("Your Name")
This is an in-line comment:1
2
3name = "Your Name"
print(name) # Here is an in-line comment!
print("Your Name")
Here is a multiline comment that uses three apostrophes above and below the comment:1
2
3
4
5
6'''
This is a multiline comment for longer comments that you want to put in your code. Commenting code is important when you're sharing code with multiple people. Because we're using the three apostrophes, we can make this comment as looooooooooong as we want!
'''
name = "Your Name"
print(name)
print("Your Name")
Wrap-up
We just covered a LOT of material. Remember:
- We’ve just barely scratched the surface here. We have have skipped some things. That’s ok! This is supposed to be an introduction. Over time, you will fill in the gaps.
- If something is unclear, ask for help. That’s why we’re here.
- If you don’t understand something or feel overwhelmed, keep at it. This is just the beginning of a long journey towards learning the ins and outs of Python.
Additional Resources
There is no shortage of additional resources on Python. It helps to read multiple articles and tutorials on the core concepts mentioned above, so we encourage you to read and explore the following: