26/October - Introduction to Python
Pre Requisites
None for today!
Class Curriculum
Section content | Expected time (mins) | Pre - Requirements |
---|---|---|
Check-in and questions from last class | 5 minutes | β |
Lesson Goals | 5 minutes | β |
Python Overview | 15 minutes | β |
Hands-on Python exercise (in pairs) | 45-60 minutes | β |
Break | 10 minutes | β |
Synthesis of Python Concepts from Exercise | 15-30 minutes | β |
(Possible) "Hello World" in Python Exercise | 20 minutes | β |
Check-out | 5 minutes | β |
Lesson Goal
Have a high-level idea of what Python is and start getting comfortable reading and writing basic statements in this programming language.
0. Python Overview
Q&A Intro
-
What is Python?
Python is a high-level programming language
-
What is a high-level programming language?
- "program": writing instructions for your computer to execute; this could be a simple "calculator" program that adds two numbers, it could be something that prints out values to your terminal or sends information to a remote server/web-page, etc.
- "programming language": the language (set of words used according to specific syntax rules) that lets you write a program that can be executed on a machine
- "high-level": there are different ways of interacting with the computer; at the lowest level, this is the binary code that computers can read and execute. Above that are different levels of closeness to machine-readable code: directly above is machine or assembly code that is slightly more readable but also compiles into this binary code for computers. There are more levels above this, and at the highest level (like Python), code words in human-readable languages (like English) represent large series of simple assembly steps. You can write some complex set of loops, etc. and the "Python interpreter" "interprets" these instructions in Python, turning them into something that the machine can comprehend. You don't have to worry about managing how your computer stores memory (more on this later), since Python takes care of this.
-
What are terms that you may hear when discussing programs and programming languages?
- "abstraction": something that has usually "hidden away" the inner complexities of a system; usually simplifies how we understand something.
- "under the hood": the details of how something works when you look past the "layers of abstraction" (see above); digging into the numerous, smaller steps of how a program, application, etc. actually functions
- "black box": a system whose inner-workings you do not understand; all you can see are the inputs and outputs. [ inputs -> BLACK_BOX -> outputs ]
-
How and in what fields is Python typically used?
- Data science: Python has many libraries (including
pandas
) that are useful for doing data analysis or statistics. Can be very efficient in how fast it computes data, and is for many people easier to read compared with other programming languages. - Machine Learning, Deep Learning: Python notebooks and libraries (including
TensorFlow
andkeras
) make it easy and clear to create neural networks and train machine learning models for prediction and generation tasks. - Running systems and infrastructure: SREs (Site Reliability Engineers) often use python as a scripting language to quickly examine, configure, and maintain infrastructure systems. Compared with other languages, it takes little code/time to write and execute a function in Python.
- Data science: Python has many libraries (including
-
What examples of things you can do with Python?
- Write programs of all kinds!
- Write a backend server for your web application, to process (receive, send) HTTP requests
- Use pandas to analyze a data set
- Create and train a neural network or other machine learning/deep learning model
- ???
-
What questions do you have so far?
1. Hands-on Python Activity
Go through this Exercise at your own pace in your groups. Make sure you are all doing this together and asking questions.
There is no rush -- it is completely fine not to finish the whole exercise!!
The purpose here is to start getting familiar with Python syntax through trial-and-error (so getting things wrong is completely expected and part of this process!). After this section, we will go through the activity as a group and discuss our observations of rules and concepts from Python that we took from the exercise.
When you get an answer wrong, make sure you look at the correct answer below and try to understand what the expression means from the correct answer.
Refresher of some math operators that you will see:
+
: addition-
: subtraction*
: multiplicationa < b
: less than --> "a is less than b"a > b
: greater than --> "a is greater than b"
2. Python Concepts - Sharing & Synthesizing
We will go over many of these concepts in more detail in the coming weeks, so no problem if it takes a little time for them to make sense!
One note about the code snippets below: in Python, we can write comments by using the #
symbol; anything after this symbol is not interpreted as code or executed. This can be a really convenient way to write notes to yourselves (or future developers!) when trying to understand code you wrote.
- What observations did you have from the exercise?
Data Types
-
What is a data type?
-
What are examples of different data types that you saw?
- strings (ex:
'asdf'
,'hello my name is x'
,'a'
,'123'
) - integers (ex:
1
,-100
,1383234
) - floats (floating point values) (ex:
1.0
,-100.1234
,0.0000003
,1e5
) - booleans (ex:
True
,False
) - lists (ex:
[3, 1, 'r', 'e']
,[]
,[3.2, 3, 4.5]
) - functions (ex:
def my_function (a, b): ...
)
(more data types that weren't shown in this exercise will be discussed in later sessions)
- strings (ex:
Variables
-
What is a variable? What's an example?
Some word (starting with a letter, and can contain letters, numbers, and underscore
_
) that represents some value. We set the value of a variable with=
, for example:test_var = 10
sets the variabletest_var
to be equal to the value of10
. Setting the value of a variable is often called assigning a variable.
Iterables (lists, strings, ...)
-
What does the
len
keyword do? i.e.len('asdf')
orlen([1,2,3,4])
len
gets the length of any "iterable" -- any "thing" that is made up of other things that you can "iterate" or count through: list, string, tuple, dictionary, set... -
What does the
[#]
syntax do?This indexes into an iterable and gets you the
#
value of it. Note that the "first" index is "0", because we 0-index in Python. For example:1 a = 'asdf' 2 b = [1,2,3,4] 3 a[0] 4 # outputs: -> 'a' 5 b[3] 6 # outputs: -> 4
Booleans and conditions
-
What does the
==
symbol mean? What about!=
?==
is "equals",!=
is "does not equal". -
What is the difference between
=
and==
?=
is for assigning some value to a variable, while==
is for testing for equality between the two sides. -
What does the
if
keyword do?This is a conditional statement:
if CONDITION:
means: "ifCONDITION
evaluates toTrue
, then execute the code that follows directly after the statement" -
How about an
if ... else
statement?1if CONDITION_A: 2 STATEMENT_A 3else: 4 STATEMENT_B
In this example, "if
CONDITION_A
evaluates toTrue
, then executeSTATEMENT_A
. Otherwise (ifCONDITION_A
evaluates toFalse
), then executeSTATEMENT_B
. --> -
How about
if ... elif ... else
?1if CONDITION_A: 2 STATEMENT_A 3elif CONDITION_B: 4 STATEMENT_B 5else: 6 STATEMENT_C
In this example, "if
CONDITION_A
evaluates toTrue
, then executeSTATEMENT_A
only. Otherwise (ifCONDITION_A
evaluates toFalse
), then check ifCONDITION_B
evaluates toTrue
and if so, executeSTATEMENT_B
only. If bothCONDITION_A
andCONDITION_B
evaluate toFalse
, then executeSTATEMENT_C
. Note that here, we are only ever execute one of theSTATEMENT_
's, even if multiple conditions evaluate toTrue
.
Functions
-
What is a function?
1def my_test_function(a): 2 return a
A function is a sequence of steps or tasks (written in a block of code), which may or may not take in arguments. Whenever we call a function, we execute this defined set of steps (with any arguments passed in). The above example function takes in some value and simply returns, or gives us back, this value when we call it like so:
my_test_function(12) # evaluates to 12
3. (if there is time) "Hello, World!" in Python
It's something of a tradition in the Computer Science world when learning a new language to write code that outputs "Hello, World!". Check out this Wikipedia article for more information on why :).
For today, go ahead and try that out in this Python playground, which will allow you to execute some test code by clicking on the Play button (triangle) on the top of the console window.
Example solutions will be posted in a few days :)
Task 0: write code that outputs "Hello, World!"
Tip: there is a built-in print
function in Python that takes some input and outputs this to standard out (STDOUT
), which is in this case the right side of the playground console.
(Recall that the #
stands for a "comment" in Python, meaning that this line is just a "note" to yourself and does not need to be included.)
Example usage:
1print("asdf asdf")
2# outputs -> asdf asdf
3print("my name is", "Robot")
4# outputs -> my name is Robot
A Solution:
1print("Hello, World!")
Task 1: create and call a function that prints "Hello, World!"
A Solution:
1def hello_world():
2 print("Hello, World!")
3
4# Let's call the function
5hello_world()
Task 2: create a function that returns the string "Hello, World!". Call this function and print the output.
A solution:
1def hello_world():
2 return "Hello, World!"
3
4print(hello_world())
Task 3: modify your function so you can greet a specific name: "Hello, Person"
In other words:
1my_hello_world("Noor")
2# expected output -> "Hello, Noor"
A solution:
1def hello_world(name):
2 return ("Hello, " + name)
3
4print(hello_world("Noor"))
5# output -> "Hello, Noor"
6print(hello_world("Jose"))
7# output -> "Hello, Jose"
Another solution:
1def hello_world(name):
2 print("Hello, " + name)
3
4hello_world("Noor")
5# output -> "Hello, Noor"
6hello_world("Jose")
7# output -> "Hello, Jose"
Additional practice (all optional)
- Modify your "greeter" using any of the concepts we discussed; perhaps include a conditional expression (i.e. only greet IF ...).
- Try to write and evaluate any Python expressions that you saw in the silent teacher activity in the playground.
- Write your own function to return or print some value.