SlideShare a Scribd company logo
1 of 26
www.teachingcomputing.com
Mastering Programming in Python
Lesson 2
• Introduction to the language, SEQUENCE variables, create a Chat bot
• Introduction SELECTION (if else statements)
• Introducing ITERATION (While loops)
• Introducing For Loops
• Use of Functions/Modular Programming
• Introducing Lists /Operations/List comprehension
• Use of Dictionaries
• String Manipulation
• File Handling – Reading and writing to CSV Files
• Importing and Exporting Files
• Transversing, Enumeration, Zip Merging
• Recursion
• Practical Programming
• Consolidation of all your skills – useful resources
• Includes Computer Science theory and Exciting themes for every
lesson including: Quantum Computing, History of Computing,
Future of storage, Brain Processing, and more …
Series Overview
*Please note that each lesson is not bound to a specific time (so it can be taken at your own pace)
Information/Theory/Discuss
Task (Code provided)
Challenge (DIY!)
Suggested Project/HW
In this lesson you will …
 Learn about conditional logic/control
flow/selection using IF ELSE statements
 Look at the use of Selection in Game Design
 Learn about Debugging/Error checking
 Analyse a Flow Chart (which has Selection in it)
 Discuss Video Gaming – Addiction
 Create a password checker
 Create a username and password (login) app
 Learn about the use of ELIF
 Learn about Boolean Variables
 Learn about Multiple Comparisons using and/or
Conditional Logic – we use it all the time!
IF Mr X is kind and helpful, THEN, we like him
ELSE we like him less!
Some philosophers will argue that all love is conditional. Others would say
nothing is truly unconditional(except God’s love – known as Agape in Greek) By
‘conditional’ we mean that an outcome depends on certain conditions.
In Python programming ELIF statements are
also used – short for ELSE AND IF
IF we are full (after dinner) THEN we go without
dessert, ELSE, we eat the apple pie!
In programming we can control the flow of a
program by using SELECTION (IF ELSE/ ELIF
statements, comparisons or Boolean variables)
Did you know?
A software bug is an error or flaw in a
computer program.
As a programmer, you are going to come
up a lot of these and it is important that
you are patient and persevere!
The first ever bug was an actual bug!
Operators traced an error in the Mark II to
a moth trapped in a relay, coining the
term bug. This bug was carefully removed
and taped to the log book
Stemming from this we now call glitches in
a computer a bug.
A page from the Harvard Mark IIelectromechanical computer's log,
featuring a dead moth that was removed from the device
Examples of IF…ELSE in game design.
It would be hard to think about creating a game without using SELECTION (if else
statements). You probably remember first using these in SCRATCH (if else blocks)
Pong (marketed as PONG) is one of the earliest arcade
video games and the very first sports arcade video game
What sort of if statements would be
needed in PONG?
An example of the use of Selection (IF)
Ifthe player has run out of lives, he…..dies!
https://youtu.be/GxL07giAC3M
Speaking of Video
games, the
following short
documentary on
Video Game
addition may be of
some interest.
What do you think?
Can Video Games
ruin people’s lives?
Can you follow the logic of this flow chart?
You will often need to create flow charts to show the logic behind your program.
Can you make any sense of the following flow chart? What is it trying to say?
If x < y
If y < x
Output: “Done”
Output “x is less
than y”
Output “y is less
than x”
Input x
Input y
5
7
Read more about Flow charts here: https://en.wikipedia.org/wiki/Flowchart
A few things to keep in mind …
Q1: If password !=“moose” then …. (In this code, what do you think “ != “ means?
!= is the
operator for
NOT equal to.
Q2: Also, do you know the difference between a = 2 and if a ==2 ?
Answer 1: Answer 2:
Use = when you are assigning
a value. (e.g. a = 2) and == is
used when you are CHECKING
to see if two values are equal.
(e.g. if a==2 then do x,y,z )
It’s very important not to get
the two confused!
Task 1: Create a password checker
1. Open a Python
Module
2. Copy and paste
the code on the
right into the
module
3. Run the program
to see what it
does
4. See if you can fix
the errors to get it
to work.
#This doesn't quite work - lots of errors. Fix them (mostly syntax and
indentation errors) and get the program to work!
#This doesn't quite work - lots of errors. Fix them (mostly syntax and
indent error) and get the program to work!
password="open123"
print('Enter password:')
answer=input()
if answer==password:
print("yes")
else
print("no")
Task 1: Solution
password="open123"
print('Enter password:')
answer=input()
if answer==password:
print("yes")
else:
print("no")
Common errors to look for.
Speech marks when referring to the data type STRING
In Python 3, don’t forget the brackets when printing text
It’s easy to forget to use the double equal signs in Python
Identation is key in Python. Make sure the IF and ELSE are
on the same indent line.
Similarlty the PRINT (within the IF and ELSE statements)
need to be indented.
Note: Sometimes we put bits of code in a function. In
Python a function is defined with the word: ‘def’ (see
below to call a function and run the code)
def login():
username="username1"
print('Enter username')
answer1=input()
print('Enter password:')
if answer1 == username and answer2 =
password:
print("Access Granted")
else:
print("Sorry, Access Denied")
Challenge 1: Username and psswd check.
1. Open a Python
Module
2. Copy and paste
the code on the
right into the
module
3. Here we use a
function so in the
shell type “login()”
to RUN the
program
4. Get it to work!
#Task - create a program which asks the user for their username AND
password.
#only if BOTH are correct, access is granted, else any other combination
results in access denied
def login():
username="username1"
print('Enter username')
answer1=input()
print('Enter password:')
if answer1 == username and answer2 = password:
print("Access Granted")
else:
print("Sorry, Access Denied")
Challenge 1: Solution
def login():
username="username1"
password="open123"
print('Enter username')
answer1=input()
print('Enter password:')
answer2=input()
if answer1 == username and answer2 == password:
print("Access Granted")
else:
print("Sorry, Access Denied")
Declare a password variable and assign it a value!
Declare ‘answer2’ as a variable for user input.
Don’t forget the double equals sign here.
Remember when you are checking equivalence a
“==“ is used, but when it is a simple assignment
operator, a single equals sign “=“ will suffice,
Task 2: Check number using ELIF
1. Open a Python
Module
2. Copy and paste
the code on the
right into the
module
3. Here we use a
function so in the
shell type
“checknum()” to
RUN the program
4. Analyse the code
def checknumber():
# In this program, we input a number
# check if the number is positive or
# negative or zero and display
# an appropriate message
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Challenge 2: Create a grade checker app
1. Open a Python
Module
2. Using the previous
code in Task 2,
see if you can
create a function
called
‘gradechecker’
3. The pseudo code
for the program is
on the right.
4. Can you do it!?
1. Ask the user for their grade (which must be between 0 and 100)
2. Allow the user to input their response (an integer response between 0
and 100)
3. If the user enters anything under 50 (<50) then print “Fail”
4. If the user enters anything above 50 (>50) then print “Pass”
Extension: If you’ve done the above, see if you can define more speficic
grade boundaries.
e.g. 0 – 20 = F
20 – 40 = E
40 –60 = D
60 – 70 = C
And so on …*You may need to do some research to complete this task!
Challenge 2: Solution(s)
def gradechecker():
# In this program,
we input a number
# check if the number
is positive or
# negative or zero
and display
# an appropriate
message
num =
float(input("Enter a
number: "))
if num > 50:
print("Pass")
elif num < 50:
print("Failed”)
Challenge 2 Solution Extension Solution
Using And / Or with IF statements
You can check multiple conditions by chaining together comparisons with and /or
#Example of AND
If x < y and x < z:
print (“x is less than y and z)
Example 1 Example 2
#Non Exclusive Or
If x < y or x < z:
print(“x is less than either y or z”)
Boolean Variables with IF statements
Python supports Boolean variables. They are basically variables that can either
store the value “True” or “False”. When using an IF statement, the expression
needs to evaluate to either True or False
In the following
example, the
variable
seat_booked is set
to ‘True’. What
would happen if it
was changed to
‘False’ ?
ROBOTICS AND IF STATEMENTS
As an extension, check out the following website on Robotics and IF statements
http://arcbotics.com/lessons/if-statements/
Challenge: Extend your Chabot from
Lesson 1 using Selection and Functions.
Here are some
suggestions, but get
creative and implement
your own ideas!
**********Get the computer to
ask the user for an age. IF the
age is above 16, take them to
another function where the
program continues, ELSE, quit
the program and inform the user
that they are too young.
#This is a chatbot and this is a comment, not executed by the program
#Extend it to make the computer ask for your favourite movie and respond
accordingly!
print('Hello this is your computer...what is your favourite number?')
#Declaring our first variable below called 'computerfavnumber' and storing
the value 33
computerfavnumber=33
#We now declare a variable but set the variable to hold whatever the
*user* inputs into the program
favnumber=input()
print(favnumber + '...is a very nice number indeed. So...what is your
name?')
name=input()
print('what a lovely name: ' + name + '...now, I will reveal what my
favourite number is:')
print (computerfavnumber)
The official Python Site (very useful!)
Make a note of it and check out the tutorial! You can find what you’re looking for, in this
case, IF statements.
Use the glossary to reference something..
Useful Videos to watch on covered topics
https://www.youtube.com/watch?v=II5WTVvryvk
More on Robotics and AI today … Recommended video on Python Control Flow
https://www.youtube.com/watch?v=vMHfUWkELg0
Suggested Project / HW / Research
 Create a research information point on the history and future of AI
 Create a timeline of Artificial Intelligence
 What are the applications of Artificial Intelligence in today’s society?
 What are the advancements in Robotics today?
 What is the future of AI and Robotics?
 What countries are currently at the forefront in these crucial fields?
 Compare and contrast the use of SELECTION in four different high level
languages (suggested: Python, VB.Net, C++, Java)
 Give examples of the syntax of if statements in each language
 Look up ‘Case statements’ in VB.Net – what is the equivalent in Python
 Contrast and compare the advantages of Python and VB.Net as languages? Which
do YOU think is better and why?
Useful links and additional reading
http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/index.html
http://www.python-course.eu/variables.php
http://www.programiz.com/python-programming/variables-datatypes
http://www.tutorialspoint.com/python/python_variable_types.htm
https://en.wikibooks.org/wiki/Python_Programming/Variables_and_Strings

More Related Content

What's hot

PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTESNi
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Python - Introduction
Python - IntroductionPython - Introduction
Python - Introductionstn_tkiller
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming LanguageDr.YNM
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPTShivam Gupta
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| FundamentalsMohd Sajjad
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning ParrotAI
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Jaganadh Gopinadhan
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming LanguageTahani Al-Manie
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data sciencedeepak teja
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAMaulik Borsaniya
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
 
Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)IoT Code Lab
 

What's hot (20)

PYTHON NOTES
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Python - Introduction
Python - IntroductionPython - Introduction
Python - Introduction
 
Python made easy
Python made easy Python made easy
Python made easy
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Python Workshop
Python WorkshopPython Workshop
Python Workshop
 
Python
PythonPython
Python
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning
 
Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming Language
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
 
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYAChapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
 
Python ppt
Python pptPython ppt
Python ppt
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)Chapter 0 Python Overview (Python Programming Lecture)
Chapter 0 Python Overview (Python Programming Lecture)
 
Python second ppt
Python second pptPython second ppt
Python second ppt
 

Viewers also liked

Workshop on Programming in Python - day II
Workshop on Programming in Python - day IIWorkshop on Programming in Python - day II
Workshop on Programming in Python - day IISatyaki Sikdar
 
CoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseCoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseAlexander Galkin
 
SmartBoard Lesson Plans
SmartBoard Lesson PlansSmartBoard Lesson Plans
SmartBoard Lesson Plansgeorgekn
 
5 lesson 2 1 variables & expressions
5 lesson 2 1 variables & expressions5 lesson 2 1 variables & expressions
5 lesson 2 1 variables & expressionsmlabuski
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Matt Harrison
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Chia-Chi Chang
 
Java OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaJava OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaOUM SAOKOSAL
 
Meetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en pythonMeetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en pythonArthur Lutz
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MoreMatt Harrison
 
Operator Overloading
Operator Overloading  Operator Overloading
Operator Overloading Sardar Alam
 
Installing Python on Mac
Installing Python on MacInstalling Python on Mac
Installing Python on MacWei-Wen Hsu
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonYi-Fan Chu
 
Lesson1 python an introduction
Lesson1 python an introductionLesson1 python an introduction
Lesson1 python an introductionArulalan T
 

Viewers also liked (20)

Workshop on Programming in Python - day II
Workshop on Programming in Python - day IIWorkshop on Programming in Python - day II
Workshop on Programming in Python - day II
 
Primitive Data Types and Variables Lesson 02
Primitive Data Types and Variables Lesson 02Primitive Data Types and Variables Lesson 02
Primitive Data Types and Variables Lesson 02
 
CoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming courseCoderDojo: Intermediate Python programming course
CoderDojo: Intermediate Python programming course
 
SmartBoard Lesson Plans
SmartBoard Lesson PlansSmartBoard Lesson Plans
SmartBoard Lesson Plans
 
5 lesson 2 1 variables & expressions
5 lesson 2 1 variables & expressions5 lesson 2 1 variables & expressions
5 lesson 2 1 variables & expressions
 
Lesson plan
Lesson planLesson plan
Lesson plan
 
Lesson 3: Variables and Expressions
Lesson 3: Variables and ExpressionsLesson 3: Variables and Expressions
Lesson 3: Variables and Expressions
 
Introduction to Advanced Javascript
Introduction to Advanced JavascriptIntroduction to Advanced Javascript
Introduction to Advanced Javascript
 
Python - Lecture 1
Python - Lecture 1Python - Lecture 1
Python - Lecture 1
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
 
Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)Learning notes of r for python programmer (Temp1)
Learning notes of r for python programmer (Temp1)
 
Java OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaJava OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to Java
 
Meetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en pythonMeetup Python Nantes - les tests en python
Meetup Python Nantes - les tests en python
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 
Operator Overloading
Operator Overloading  Operator Overloading
Operator Overloading
 
Installing Python on Mac
Installing Python on MacInstalling Python on Mac
Installing Python on Mac
 
Python for All
Python for All Python for All
Python for All
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Lesson1 python an introduction
Lesson1 python an introductionLesson1 python an introduction
Lesson1 python an introduction
 
Python master class part 1
Python master class part 1Python master class part 1
Python master class part 1
 

Similar to Mastering python lesson2

Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowRanel Padon
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptxAnum Zehra
 
Application development with Python - Desktop application
Application development with Python - Desktop applicationApplication development with Python - Desktop application
Application development with Python - Desktop applicationBao Long Nguyen Dang
 
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelpmeinhomework
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonmckennadglyn
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2Dan D'Urso
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answersRojaPriya
 
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Lucky Gods
 
Python for Physical Science.pdf
Python for Physical Science.pdfPython for Physical Science.pdf
Python for Physical Science.pdfMarilouANDERSON
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answerskavinilavuG
 
Learn python
Learn pythonLearn python
Learn pythonmocninja
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonRanjith kumar
 
265 ge8151 problem solving and python programming - 2 marks with answers
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answersvithyanila
 

Similar to Mastering python lesson2 (20)

Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
 
ForLoops.pptx
ForLoops.pptxForLoops.pptx
ForLoops.pptx
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Introduction To Python.pptx
Introduction To Python.pptxIntroduction To Python.pptx
Introduction To Python.pptx
 
Application development with Python - Desktop application
Application development with Python - Desktop applicationApplication development with Python - Desktop application
Application development with Python - Desktop application
 
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming Homework
 
_PYTHON_CHAPTER_2.pdf
_PYTHON_CHAPTER_2.pdf_PYTHON_CHAPTER_2.pdf
_PYTHON_CHAPTER_2.pdf
 
Python Session - 6
Python Session - 6Python Session - 6
Python Session - 6
 
pyton Notes1
pyton Notes1pyton Notes1
pyton Notes1
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
INTERNSHIP REPORT.docx
 INTERNSHIP REPORT.docx INTERNSHIP REPORT.docx
INTERNSHIP REPORT.docx
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
 
Python for Physical Science.pdf
Python for Physical Science.pdfPython for Physical Science.pdf
Python for Physical Science.pdf
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Learn python
Learn pythonLearn python
Learn python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
265 ge8151 problem solving and python programming - 2 marks with answers
265   ge8151 problem solving and python programming - 2 marks with answers265   ge8151 problem solving and python programming - 2 marks with answers
265 ge8151 problem solving and python programming - 2 marks with answers
 

Recently uploaded

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 

Recently uploaded (20)

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 

Mastering python lesson2

  • 2. • Introduction to the language, SEQUENCE variables, create a Chat bot • Introduction SELECTION (if else statements) • Introducing ITERATION (While loops) • Introducing For Loops • Use of Functions/Modular Programming • Introducing Lists /Operations/List comprehension • Use of Dictionaries • String Manipulation • File Handling – Reading and writing to CSV Files • Importing and Exporting Files • Transversing, Enumeration, Zip Merging • Recursion • Practical Programming • Consolidation of all your skills – useful resources • Includes Computer Science theory and Exciting themes for every lesson including: Quantum Computing, History of Computing, Future of storage, Brain Processing, and more … Series Overview *Please note that each lesson is not bound to a specific time (so it can be taken at your own pace) Information/Theory/Discuss Task (Code provided) Challenge (DIY!) Suggested Project/HW
  • 3. In this lesson you will …  Learn about conditional logic/control flow/selection using IF ELSE statements  Look at the use of Selection in Game Design  Learn about Debugging/Error checking  Analyse a Flow Chart (which has Selection in it)  Discuss Video Gaming – Addiction  Create a password checker  Create a username and password (login) app  Learn about the use of ELIF  Learn about Boolean Variables  Learn about Multiple Comparisons using and/or
  • 4. Conditional Logic – we use it all the time! IF Mr X is kind and helpful, THEN, we like him ELSE we like him less! Some philosophers will argue that all love is conditional. Others would say nothing is truly unconditional(except God’s love – known as Agape in Greek) By ‘conditional’ we mean that an outcome depends on certain conditions. In Python programming ELIF statements are also used – short for ELSE AND IF IF we are full (after dinner) THEN we go without dessert, ELSE, we eat the apple pie! In programming we can control the flow of a program by using SELECTION (IF ELSE/ ELIF statements, comparisons or Boolean variables)
  • 5. Did you know? A software bug is an error or flaw in a computer program. As a programmer, you are going to come up a lot of these and it is important that you are patient and persevere! The first ever bug was an actual bug! Operators traced an error in the Mark II to a moth trapped in a relay, coining the term bug. This bug was carefully removed and taped to the log book Stemming from this we now call glitches in a computer a bug. A page from the Harvard Mark IIelectromechanical computer's log, featuring a dead moth that was removed from the device
  • 6. Examples of IF…ELSE in game design. It would be hard to think about creating a game without using SELECTION (if else statements). You probably remember first using these in SCRATCH (if else blocks) Pong (marketed as PONG) is one of the earliest arcade video games and the very first sports arcade video game What sort of if statements would be needed in PONG?
  • 7. An example of the use of Selection (IF) Ifthe player has run out of lives, he…..dies! https://youtu.be/GxL07giAC3M Speaking of Video games, the following short documentary on Video Game addition may be of some interest. What do you think? Can Video Games ruin people’s lives?
  • 8. Can you follow the logic of this flow chart? You will often need to create flow charts to show the logic behind your program. Can you make any sense of the following flow chart? What is it trying to say? If x < y If y < x Output: “Done” Output “x is less than y” Output “y is less than x” Input x Input y 5 7 Read more about Flow charts here: https://en.wikipedia.org/wiki/Flowchart
  • 9. A few things to keep in mind … Q1: If password !=“moose” then …. (In this code, what do you think “ != “ means? != is the operator for NOT equal to. Q2: Also, do you know the difference between a = 2 and if a ==2 ? Answer 1: Answer 2: Use = when you are assigning a value. (e.g. a = 2) and == is used when you are CHECKING to see if two values are equal. (e.g. if a==2 then do x,y,z ) It’s very important not to get the two confused!
  • 10. Task 1: Create a password checker 1. Open a Python Module 2. Copy and paste the code on the right into the module 3. Run the program to see what it does 4. See if you can fix the errors to get it to work. #This doesn't quite work - lots of errors. Fix them (mostly syntax and indentation errors) and get the program to work! #This doesn't quite work - lots of errors. Fix them (mostly syntax and indent error) and get the program to work! password="open123" print('Enter password:') answer=input() if answer==password: print("yes") else print("no")
  • 11. Task 1: Solution password="open123" print('Enter password:') answer=input() if answer==password: print("yes") else: print("no") Common errors to look for. Speech marks when referring to the data type STRING In Python 3, don’t forget the brackets when printing text It’s easy to forget to use the double equal signs in Python Identation is key in Python. Make sure the IF and ELSE are on the same indent line. Similarlty the PRINT (within the IF and ELSE statements) need to be indented.
  • 12. Note: Sometimes we put bits of code in a function. In Python a function is defined with the word: ‘def’ (see below to call a function and run the code) def login(): username="username1" print('Enter username') answer1=input() print('Enter password:') if answer1 == username and answer2 = password: print("Access Granted") else: print("Sorry, Access Denied")
  • 13. Challenge 1: Username and psswd check. 1. Open a Python Module 2. Copy and paste the code on the right into the module 3. Here we use a function so in the shell type “login()” to RUN the program 4. Get it to work! #Task - create a program which asks the user for their username AND password. #only if BOTH are correct, access is granted, else any other combination results in access denied def login(): username="username1" print('Enter username') answer1=input() print('Enter password:') if answer1 == username and answer2 = password: print("Access Granted") else: print("Sorry, Access Denied")
  • 14. Challenge 1: Solution def login(): username="username1" password="open123" print('Enter username') answer1=input() print('Enter password:') answer2=input() if answer1 == username and answer2 == password: print("Access Granted") else: print("Sorry, Access Denied") Declare a password variable and assign it a value! Declare ‘answer2’ as a variable for user input. Don’t forget the double equals sign here. Remember when you are checking equivalence a “==“ is used, but when it is a simple assignment operator, a single equals sign “=“ will suffice,
  • 15. Task 2: Check number using ELIF 1. Open a Python Module 2. Copy and paste the code on the right into the module 3. Here we use a function so in the shell type “checknum()” to RUN the program 4. Analyse the code def checknumber(): # In this program, we input a number # check if the number is positive or # negative or zero and display # an appropriate message num = float(input("Enter a number: ")) if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")
  • 16. Challenge 2: Create a grade checker app 1. Open a Python Module 2. Using the previous code in Task 2, see if you can create a function called ‘gradechecker’ 3. The pseudo code for the program is on the right. 4. Can you do it!? 1. Ask the user for their grade (which must be between 0 and 100) 2. Allow the user to input their response (an integer response between 0 and 100) 3. If the user enters anything under 50 (<50) then print “Fail” 4. If the user enters anything above 50 (>50) then print “Pass” Extension: If you’ve done the above, see if you can define more speficic grade boundaries. e.g. 0 – 20 = F 20 – 40 = E 40 –60 = D 60 – 70 = C And so on …*You may need to do some research to complete this task!
  • 17. Challenge 2: Solution(s) def gradechecker(): # In this program, we input a number # check if the number is positive or # negative or zero and display # an appropriate message num = float(input("Enter a number: ")) if num > 50: print("Pass") elif num < 50: print("Failed”) Challenge 2 Solution Extension Solution
  • 18. Using And / Or with IF statements You can check multiple conditions by chaining together comparisons with and /or #Example of AND If x < y and x < z: print (“x is less than y and z) Example 1 Example 2 #Non Exclusive Or If x < y or x < z: print(“x is less than either y or z”)
  • 19. Boolean Variables with IF statements Python supports Boolean variables. They are basically variables that can either store the value “True” or “False”. When using an IF statement, the expression needs to evaluate to either True or False In the following example, the variable seat_booked is set to ‘True’. What would happen if it was changed to ‘False’ ?
  • 20. ROBOTICS AND IF STATEMENTS As an extension, check out the following website on Robotics and IF statements http://arcbotics.com/lessons/if-statements/
  • 21. Challenge: Extend your Chabot from Lesson 1 using Selection and Functions. Here are some suggestions, but get creative and implement your own ideas! **********Get the computer to ask the user for an age. IF the age is above 16, take them to another function where the program continues, ELSE, quit the program and inform the user that they are too young. #This is a chatbot and this is a comment, not executed by the program #Extend it to make the computer ask for your favourite movie and respond accordingly! print('Hello this is your computer...what is your favourite number?') #Declaring our first variable below called 'computerfavnumber' and storing the value 33 computerfavnumber=33 #We now declare a variable but set the variable to hold whatever the *user* inputs into the program favnumber=input() print(favnumber + '...is a very nice number indeed. So...what is your name?') name=input() print('what a lovely name: ' + name + '...now, I will reveal what my favourite number is:') print (computerfavnumber)
  • 22. The official Python Site (very useful!) Make a note of it and check out the tutorial! You can find what you’re looking for, in this case, IF statements.
  • 23. Use the glossary to reference something..
  • 24. Useful Videos to watch on covered topics https://www.youtube.com/watch?v=II5WTVvryvk More on Robotics and AI today … Recommended video on Python Control Flow https://www.youtube.com/watch?v=vMHfUWkELg0
  • 25. Suggested Project / HW / Research  Create a research information point on the history and future of AI  Create a timeline of Artificial Intelligence  What are the applications of Artificial Intelligence in today’s society?  What are the advancements in Robotics today?  What is the future of AI and Robotics?  What countries are currently at the forefront in these crucial fields?  Compare and contrast the use of SELECTION in four different high level languages (suggested: Python, VB.Net, C++, Java)  Give examples of the syntax of if statements in each language  Look up ‘Case statements’ in VB.Net – what is the equivalent in Python  Contrast and compare the advantages of Python and VB.Net as languages? Which do YOU think is better and why?
  • 26. Useful links and additional reading http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/index.html http://www.python-course.eu/variables.php http://www.programiz.com/python-programming/variables-datatypes http://www.tutorialspoint.com/python/python_variable_types.htm https://en.wikibooks.org/wiki/Python_Programming/Variables_and_Strings

Editor's Notes

  1. Associated Resource: Python GUI Programming: See www.teachingcomputing.com (resources overview) for more information
  2. Associated Resource: Python GUI Programming: See www.teachingcomputing.com (resources overview) for more information
  3. Image source: http://www.peardeck.com
  4. Image(s) source: Dreamstime.com (royalty free images)
  5. Image(s) source: wikipedia
  6. Image(s) source: Dreamstime.com (royalty free images)
  7. Image Source: https://s-media-cache-ak0.pinimg.com
  8. Image(s) source: Dreamstime.com (royalty free images)
  9. Image(s) source: Dreamstime.com (royalty free images)
  10. Teachers: You may want to delete this slide if you are giving the power point to your students!
  11. Image(s) source: Dreamstime.com (royalty free images)
  12. Image(s) source: Dreamstime.com (royalty free images)
  13. Source: taken from BBC news article that can be read here: http://www.bbc.co.uk/news/35501537
  14. Image(s) source: Wikipedia
  15. Image(s) source: Wikipedia