24/March - 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?
-
What is a high-level programming language?
-
What are terms that you may hear when discussing programs and programming languages?
-
How and in what fields is Python typically used?
-
What examples of things you can do with Python?
- 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
Task 1: create and call a function that prints "Hello, 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"
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.