0% found this document useful (0 votes)
33 views40 pages

Class 2 P

The document discusses Python programming concepts like variables, data types, operators, strings, printing, lists, dictionaries, sets, conditional statements, loops and functions. It provides examples and exercises for different concepts. It also discusses a few programming tasks like calculating remaining life expectancy and splitting a bill.

Uploaded by

armaan.dinajpur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views40 pages

Class 2 P

The document discusses Python programming concepts like variables, data types, operators, strings, printing, lists, dictionaries, sets, conditional statements, loops and functions. It provides examples and exercises for different concepts. It also discusses a few programming tasks like calculating remaining life expectancy and splitting a bill.

Uploaded by

armaan.dinajpur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

RECAP

Variables
Types
Arithmetic operators
Boolean logic
Strings
Printing
Exercises
AGENDA
Lists
Dictionaries
Sets
If Else
Loops
Functions
Classes
Exercises
BUT FIRST A FEW TASKS
Create a program that tells us how many days, weeks, months we have left if we live until
90 years old.
It will take your current age as the input and output a message with our time left in this
format:
You have x days, y weeks, or z months left.
Where x, y and z are replaced with the actual calculated numbers.
Warning your output should match the Example Output format exactly, even the positions
of the commas and full stops.

Example Input
56
Example Output
You have 12410 days, 1768 weeks, OR 408 months left.
Instructions
If the bill was $150.00, split between 5 people, with 12% tip.
Each person should pay (150.00 / 5) * 1.12 = 33.6
Format the result to 2 decimal places = 33.60
Thus, everyone's share of the total bill is $30.00 plus a $3.60 tip.

Tip: There are 2 ways to round a number.


format(number, '.2f’) >>> format(math.pi, '.12g') # give 12 significant digits
round(number, digits) '3.14159265359'

.
>>> format(math pi, '.2f') # give 2 digits after the point
Example Input '3.14'
Welcome to the tip calculator!
What was the total bill? $124.56
How much tip would you like to give? 10, 12, or 15? 12
How many people to split the bill? 7
Example Output
Each person should pay: $19.93
LISTS
One of the most useful concepts
Group multiple variables together (a kind of container!)
INDEXING A LIST
• Indexing – accessing items within a data structure

• Indexing a list is not very intuitive...


• The first element of a list has an index 0
QUICK QUIZ
What will fruits[3] return?
QUICK QUIZ
What will this return?
DATA STRUCTURE SIZES
Make sure you are always aware of the sizes of each variable!
This can easily be done using the len() function.
It returns the length/size of any data structure
IS A TOMATO REALLY A FRUIT?

Furthermore, we can modify lists in various ways


LISTS WITH INTEGERS
range() - a function that generates a sequence of numbers as a list
SLICING LISTS
• Slicing – obtain a particular set of sub-elements from a data structure.
• Very useful and flexible.
LISTS – HELPFUL FUNCTIONS
Makes them extremely useful and versatile
LISTS CAN BE OF DIFFERENT TYPES
Not very useful, but possible
MUTABILITY
Mutable object – can be changed after creation.

Immutable object - can NOT be changed after creation.


QUICK QUIZ

Are lists mutable?


TUPLES
Effectively lists that are immutable (i.e., can't be changed)
DICTIONARIES Tanmoy1
• Similar to actual dictionaries
• They are effectively 2 lists
combined – keys and values Nowrin
• We use the keys to access the
values instead of indexing them Kaniz
like a list
• Each value is mapped to a
Irtija
unique key

Tanmoy2
DICTIONARY DEFINITION
Defined as comma separated key : value pairs:

Comma separated

Curly brackets
DICTIONARY PROPERTIES
Values are mapped to a key
Values are accessed by their key
Key are unique and are immutable
Values cannot exist without a key
DICTIONARIES
Let us define the one from the previous image
ACCESSING A DICTIONARY
Values are accessed by their keys (just like a dictionary)

Note that they can't be indexed like a list


ALTERING A DICTIONARY
Can be done via the dictionary methods
KEYS AND VALUES
It is possible to obtain only the keys or values of a dictionary.

This is useful for iteration.


SETS
Effectively lists that cannot contain duplicate items
Similar functionality to lists
Can't be indexed or sliced
Can be created with {} or you can convert a list to a set
IF ELSE
Fundamental building block of software

Conditional statement
Executed if answer is True

Executed if answer is False


IF ELSE EXAMPLE
Try running the example below.
What do you get?
INDENTATION MATTERS!
Code is grouped by its indentation
Indentation is the number of whitespace or tab characters before the code.
If you put code in the wrong block then you will get unexpected behaviour
EXTENDING IF-ELSE BLOCKS
We can add infinitely more if statements using elif

elif = else + if which means that the previous statements must be false for the current one
to evaluate to true
Write a program that works out whether if
a given number is an odd or even number.
Multiple if if and elif Nested if
If condition If condition
code code If condition
If condition elif condition If condition
code code If condition
If condition elif condition code
code code
If condition elif condition else:
code code code

else: else:
code code
BITCOIN BROKER EXAMPLE
QUICK QUIZ
What would happen if both conditions are True?
FOR LOOP
Allows us to iterate over a set amount of variables within a data structure. During
that we can manipulate each item however we want

Again, indentation is important here!


EXAMPLE
Say we want to go over a list and print each item along with its index

What if we have much more than 4 items in the list, say, 1000?
FOR EXAMPLE
• Now with a for loop

• Saves us writing more lines


• Doesn't limit us in term of size
NUMERICAL FOR LOOP
WHILE LOOP
Another useful loop. Similar to the for loop.
A while loop doesn't run for a predefined number of iterations, like a for loop. Instead, it stops
as soon as a given condition becomes true/false.
FURTHER READING
LinkedIn Learning:
Python Essentials Training – more detailed; good if you want to use your own
environment – https://www.linkedin.com/learning/python-essential-training-
2/welcome?u=50251009&auth=true
Python for Data Science – continues this course; taught with Jupyter Notebooks as
well – https://www.linkedin.com/learning/python-for-data-science-essential-
training-part-1/data-science-life-hacks?u=50251009&auth=true
Python Crash Course book – more detailed; more exercises
Python Data Analysis – O'Reilly press
PEP 8 style: https://www.python.org/dev/peps/pep-0008/

You might also like