{python}

Introduction to Python - Lecture 2

Zohair ul Hasan

Recap

  • Examples of using programming to solve real world issues
  • Set up development environment on Replit for writing code

Today's Lecture

In this lecture, we will talk about:

 

  • Programming
  • Python
  • The Python Shell
  • Datatypes
  • String Concatenation and Replication
  • Variables
  • Assignment Statements

What is Programming?

For example:

  • “Do this; then do that.”
  • “If this condition is true, perform this action; otherwise, do that action.”
  • “Do this action exactly 27 times.”
  • “Keep doing that until this condition is true.” 

Act of entering instructions into the computer to perform

What does this program do?

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')

What is Python?

Python is one of many programming languages

What is Python?

If we write Python, we'll have to follow its "syntax" (grammar/rules for writing), otherwise our program will throw an error

What is Python?

Python has an "interpreter", which is a special program that can read, understand and "execute" Python

What is Python?

print("Hello World!")

Python code

Binary

11011000110

How to program

Break down a big task into simple instructions that a computer can understand

Pretend you're talking to a 5 year old

How to program

For example, how would you tell a 5 year old how to make a jam sandwich

  • Gather Ingredients and Tools:

    • Bread slices: 2
    • Jam: 1 jar
    • Butter knife: 1
    • Plate: 1
  • Place First Bread Slice on Plate:

    • Take one slice of bread.
    • Place the bread slice flat on the plate.
  • Spread Jam on First Bread Slice:

    • Take the butter knife.
    • Insert the knife into the jam jar.
    • Scoop out a small amount of jam (approximately 1 tablespoon).
    • Remove the knife from the jar.
    • Spread the jam evenly on the top surface of the bread slice on the plate.

 

  • Place Second Bread Slice on Top:
    • Take the second slice of bread.
    • Place it directly on top of the jam-covered side of the first bread slice. Align the edges of both slices.
  • Finalize Sandwich:

    • Press the two slices together gently to ensure they stick.
  • Output Sandwich:

    • The jam sandwich is now ready for consumption. Optionally, cut the sandwich into smaller pieces if desired.

Learning to use the Python shell

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

Learning to use the Python shell

Follow this activity to explore the shell: itp.zohair.dev/activities/python-shell

Datatypes

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

Datatypes

The most common datatypes in Python are:

 

  • Integers
  • Floating point numbers (or floats for short)
  • Strings

Datatypes

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'

Datatypes

>>> 'Hello world!'
'Hello world!'
>>> 'Hello world!
  File "<stdin>", line 1
    'Hello world!
    ^
SyntaxError: unterminated string literal (detected at line 1)

String Concatenation and Replication

Follow this activity to explore the shell: itp.zohair.dev/activities/string-concatenation

Variables

A variable is a box in the computer's memory where you can store a single value

Assignment Statements

We store values inside variables using assignment statements.

Assignment Statements

For example:

a = 52

Assignment Statements

For example:

a = 52

Variable

Assignment Statements

For example:

a = 52

Variable

Assignment Operator

Assignment Statements

For example:

a = 52

Variable

Assignment Operator

Value

Assignment Statements

For example:

a = 52

Variable

Assignment Operator

Value

Variable Name Value
a 52

Computer Memory

Variables

>>> spam = 40
>>> spam
40
>>> eggs = 2
>>> spam + eggs
42
>>> spam + eggs + spam
82
>>> spam = spam + 2
>>> spam
42

Try it out!

Variables

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

Variable Names

Use descriptive names for variables - very important for code readability

Variable Naming Rules

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 Naming Rules

Variable names are case sensitive

For example, counter, cOunter, cOUNTer, counteR are four different variables

Variable Naming Rules

Although it won't break your program if you violate this rule, but you should use snake_case for variables in Python

Running our first actual program

How to find help

Google

Quiz!