Introduction to Python - Lecture 2
Zohair ul Hasan
In this lecture, we will talk about:
For example:
Act of entering instructions into the computer to perform
passwordFile = open('SecretPasswordFile.txt')
secretPassword = passwordFile.read()
print('Enter your password.')
typedPassword = input()
if typedPassword == secretPassword:
print('Access granted')
if typedPassword == '12345':
print('That password is one that an idiot puts on their luggage.')
else:
print('Access denied')Python is one of many programming languages
If we write Python, we'll have to follow its "syntax" (grammar/rules for writing), otherwise our program will throw an error
Python has an "interpreter", which is a special program that can read, understand and "execute" Python
print("Hello World!")Python code
Binary
11011000110Break down a big task into simple instructions that a computer can understand
Pretend you're talking to a 5 year old
For example, how would you tell a 5 year old how to make a jam sandwich
Gather Ingredients and Tools:
Place First Bread Slice on Plate:
Spread Jam on First Bread Slice:
Finalize Sandwich:
Output Sandwich:
Python has an "interactive shell" (also known as the REPL (Read-Evaluate-Print Loop)
You can execute Python instructions one at a time in the shell, and it'll instantly show you the results
Follow this activity to explore the shell: itp.zohair.dev/activities/python-shell
Expressions are values combined with operators
Expressions evaluate to a single value
A datatype is a category for values
Each value belongs to one datatype
For example, the number 34 is a value of type integer
The most common datatypes in Python are:
| Data type | Examples |
|---|---|
| Integers | 1, 5, 8, -2, 0, 506 |
| Floating point number | 1.5, 3.2 6.7777, 5.0, 0.0, -6.4 |
| Strings | 'hello', 'a', "world", ' ', '56' |
>>> 'Hello world!'
'Hello world!'
>>> 'Hello world!
File "<stdin>", line 1
'Hello world!
^
SyntaxError: unterminated string literal (detected at line 1)Follow this activity to explore the shell: itp.zohair.dev/activities/string-concatenation
A variable is a box in the computer's memory where you can store a single value
We store values inside variables using assignment statements.
For example:
a = 52
For example:
a = 52
Variable
For example:
a = 52
Variable
Assignment Operator
For example:
a = 52
Variable
Assignment Operator
Value
For example:
a = 52
Variable
Assignment Operator
Value
| Variable Name | Value |
|---|---|
| a | 52 |
Computer Memory
>>> spam = 40
>>> spam
40
>>> eggs = 2
>>> spam + eggs
42
>>> spam + eggs + spam
82
>>> spam = spam + 2
>>> spam
42Try it out!
A variable is initialized the first time a value is stored in it
After a variable is initialized, you can use it in expressions alongside other variables and values
When a variable is assigned a new value, the old value is forgotten
Use descriptive names for variables - very important for code readability
It can be only one word with no spaces
It can use only letters, numbers, and the underscore (_) character
It can’t begin with a number
Variable names are case sensitive
For example, counter, cOunter, cOUNTer, counteR are four different variables
Although it won't break your program if you violate this rule, but you should use snake_case for variables in Python
Follow the activity at https://itp.zohair.dev/activities/first-program
How to find help