SlideShare a Scribd company logo
1 of 56
Download to read offline
INTRODUCTION TO PYTHON
PART 2 OF 2
Silvia Tinena Coris
Electronics & Automation Course Leader
Silvia Tinena Coris
Table of contents
Part 1:
 1. Learning Outcomes
 2. Introduction
 3. The first program: hello world!
 4. The second program: hello (your name)!
 5. More data types
 6. Important things to know
 7. If Statement
 8. The third program: boy or girl?
 9. For Statement
 10. The fourth and fifth programs: your name + hello
students!
2
Silvia Tinena Coris
Table of contents
Part 2:
 11. While Statement
 12. The sixth program: security word
 13. Functions
 14. Seventh program: my functions
 15. Interesting links
 16. Exercises
3
Continuation from Part 14
11. While Statement5
Silvia Tinena Coris
While
6
 The loop is executed as long as the condition
is true
 Operands for the condition:
 < less than
 > greater than
 == equal to
 <= less than or equal to
 >= greater than or equal to
 != different to
while (True):
…
Infinite loop!
Silvia Tinena Coris
While
7
Now, try it yourself!
Create your own loop with
some operations on it
12. Sixth program: security word8
Silvia Tinena Coris
Security word – level 1
9
Silvia Tinena Coris
Security word – level 2
10
 If the user has typed the security word wrong 5
times, then wait for 30 seconds before allowing
the user to type it again.
 To do so:
 import time (at the very beginning of the
program)
 when you want the delay: time.sleep(s)
 being ‘s’ the number of seconds you want the program to
stop
Silvia Tinena Coris
Security word – level 3
11
 If the user writes down the security word
wrong 5 times, wait for 30 seconds.
 The second time that the user writes 5 wrong
security words, wait for 60 seconds.
 The third time, wait for 90 seconds.
 The fourth, 120 seconds.
 And so on…
Silvia Tinena Coris
Security word – level 3
12
Silvia Tinena Coris
Security word – PRO
13
 Now, we are going to emulate a login system
from any website/computer
 First, you need to ask the user for a login
name
13. Functions14
Silvia Tinena Coris
Functions
15
 Useful for shortening the code and automatising
sets of actions that will be repeatedly used
together and in the same order
def function_name(internal_name_input1,
internal_name_input2):
…
print(output1, output2)
 To call it:
function_name(value_1st_input,
value_2nd_input)
Silvia Tinena Coris
Functions
16
Now, try it yourself!
Create your own function
and call it as man times as
you want with different input
values
14. Seventh program: my
functions
17
Silvia Tinena Coris
My functions
18
 Create a function sum() and a function
multiply() that sums and multiplies
(respectively) all the numbers in a list of
numbers.
 Make sure that it works with any list of
numbers, regardless of its length.
 For that, create a couple of lists which have
different lengths and try to call the function with
each.
Silvia Tinena Coris
My functions – solutions
19
Now, try it yourself!
Create your own list and
use it in a function
Silvia Tinena Coris
My functions – solutions
20
Now, try it yourself!
Create your own list and
use it in a function
15. Interesting links21
Silvia Tinena Coris
Interesting links
22
 https://docs.python.org/3/
 https://docs.python.org/2/reference/index.html
 http://www.python-
course.eu/sequential_data_types.php
 http://www.tutorialspoint.com/python/
Remember to do the flowchart and the
pseudo code BEFORE programming!!!
16. Exercises23
Silvia Tinena Coris
0: A, B, C, D or E?
24
 Take the pseudo code of the exercise where
the user is asked the result of an exam (0-100)
and the program returns the equivalent mark in
terms of A, B, C, D or E.
Silvia Tinena Coris
0: A, B, C, D or E? – solution
25
Silvia Tinena Coris
1: Histogram
26
 Create a program which, given 7 inputs, writes
down a histogram. (Note that some of the
inputs can be 0)
 eg. 2, 5, 6, 8, 10, 7, 11
**
*****
******
********
**********
*******
***********
Silvia Tinena Coris
1: Histogram – solution
27
Silvia Tinena Coris
1: Histogram – solution
28
Silvia Tinena Coris
1: Histogram – solution with for and
list29
Silvia Tinena Coris
2: 99 bottles of beer
30
 Level 1:
 Traditional song in the USA and Canada
99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the
wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down, pass it around, 97 bottles of beer on the
wall.
etc.
 Create a Python program which generates all the
verses of this song.
Silvia Tinena Coris
2: 99 bottles of beer
31
 Level 2:
 Modify the last verse like this (underlined):
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles
of beer on the wall.
No more bottles of beer on the wall, no more bottles
of beer.
Go to the store and buy some more, 99 bottles of
beer on the wall.
Silvia Tinena Coris
2: 99 bottles of beer
32
 Level 3:
 If you have developed your code using range,
now use while
 If you have developed your code using while,
now use range
 Level 4:
 Learn the song and sing it!
Silvia Tinena Coris
2: 99 bottles of beer – solution
33
 Using range
Silvia Tinena Coris
2: 99 bottles of beer – solution
34
 Using while
Silvia Tinena Coris
2: 99 bottles of beer – solution
35
Silvia Tinena Coris
3: age guess
 Level 1: start with the basic functionality
 Ask the year when the user was born
 Display the age
 Level 2:
 Polish the program asking the user if he or she has
already celebrated the birthday this year. This will
mean a ± 1 year.
 Level 3:
 Greet the user and make a sentence with his or her
name plus his or her age
 eg. (name), you’re (age) years old!
36
Silvia Tinena Coris
3: age guess
 Level 4:
 If the user has born this year, display a special
message
 eg. You are still a baby!
 Level 5:
 Avoid negative ages by displaying a message if the
user enters a year superior to that we are in (2014)
 eg. Oops! You aren’t born yet!
 Level 6:
 In the same situation as before, ask the user to come
back in X years’ time, when he or she will be born
 eg. Come back in (X) years’ time!
37
Silvia Tinena Coris
3: age guess
 Level 7:
 Instead of pressing 1 or 2 to know if the birthday
has been celebrated yet, ask the user to write yes
or no (lowercase)
 Level 8:
 Create age blocks with personalised messages
for each
 1~12: You are a child!
 13~19: You are a teenager!
 20~30: You are a youngster!
 31~65: You are an adult!
 >65: You are a senior!
38
Silvia Tinena Coris
3: age guess – solution
39
 Levels 1 to 6
Silvia Tinena Coris
3: age guess – solution
40
 Levels
7 and 8
Silvia Tinena Coris
The random function with a list
41
 1. Import the random module at the very first line
of the program.
 import random
 2. Create a list of things to be randomly picked.
This means that if you want a random animal,
you will have to create a list of animals.
 3. Assign to a variable a random value of the list.
 my_fav_animal = random.choice(animals)
 4. Now you can use my_fav_animal as usual!
Silvia Tinena Coris
The random function with a list
42
Now, try it yourself!
Create your own list
and randomly pick
one of its items
Silvia Tinena Coris
The random function with numbers
43
 1. Import the random module at the very first line
of the program.
 import random
 2. Assign to a variable a random value of the
defined range.
 random_num = random.randint(1,100)
 3. Now you can use your random_num as
usual!
Silvia Tinena Coris
The random function with numbers
44
Now, try it yourself!
Randomly pick one
number from a
defined range
Silvia Tinena Coris
4: Story time
45
 You are going to create a story which will be
different every time you run the program
 For that, we will create different lists and use
the random function
Silvia Tinena Coris
4: Story time
 Ask the user his or her name and gender and
store them in appropriate variables
 Create a variable called ‘pronoun‘ which will
obviously be he, she or it. Assign it depending
on the gender
 Create four lists:
 Roles, names, actions and places
 eg. roles = [‘knight’, ‘princess’,
‘prince’, ‘wizard’]
 They can have as many items as you want and
not all the list have to have the same number of
words
46
Silvia Tinena Coris
4: Story time
 Now write the story, all in the same line!
 Suggestion: write the story in a variable and then
do a print of this variable
 If you aren’t feeling imaginative, you can
borrow this story:
Once upon a time, there was a (role) called (user’s name).
(pronoun) and some friends found themselves in the magic
land of (place). This land was ruled by (name) the (role2).
All of a sudden a mysterious voice spoke to them from high in
the sky and said: “you must (action) (name) the (role2) to lift
the curse of not being able to use technology…”
47
Silvia Tinena Coris
4: Story time – solution
48
Silvia Tinena Coris
5: Guess the number
49
 Level 1:
 The program will randomly generate a number
between 1 and 100 and the user will have to
guess it.
 To help the user, the program will “say” whether
the number to be guessed is higher or lower than
that the user entered.
 Level 2:
 Interact with the user and display personalised
messages with his or her name.
Silvia Tinena Coris
5: Guess the number
50
 Level 3:
 Count how long does it take to the user to guess
the number and display different messages
depending if the user has guessed it in the first
round or not.
 Level 4:
 If the number guessed by the user is really close
to the actual number (let’s say it’s 5 or less units
lower or higher), display a special message
Silvia Tinena Coris
5: Guess the number – a bit of help
51
 Level 4:
 if users_num < random_num:
 if users_num – random_num <= 5
 print (‘You’re almost there! Just a little bit higher!’)
 if users_num > random_num:
 if users_num – random_num >= 5
 print (‘You’re almost there! Just a little bit lower!’)
Silvia Tinena Coris
6: Hangman
52
 I guess you all know how to play hangman!
 The user can only fail 5 times
 Level 1:
 Fixed word
 The remaining guesses are just a number on the
screen (not the actual hangman)
 Level 2:
 List of words, random choice of them
Silvia Tinena Coris
6: Hangman
53
 Level 3:
 Display a hangman. You can do one like this:
O
_|_/
|
/ 
d b
 Level 4:
 Give the option that another user writes down the secret
word! (If there is only one player, the program will choose
the secret word. If there are two players, the first user will
write the word down and the second will have to guess
it).
 Hint: after the 1st player has written down the word, introduce
a lot of spaces so the 2nd player can’t see the word!
Silvia Tinena Coris
6: Hangman – solution
54
 Levels 1 to 3
Silvia Tinena Coris
6: Hangman – solution
55
 Level 4
Any comments or questions? Drop me an email:
Silvia.tinena.coris@gmail.com
Or add me on LinkedIN:
Silvia Tinena Coris
Thanks!
Hope you’ve learned 56

More Related Content

What's hot

Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Charling Li
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Notes2
Notes2Notes2
Notes2hccit
 
Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6cmontanez
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and commentsNilimesh Halder
 
Acm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAcm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAhmad Bashar Eter
 

What's hot (7)

Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Notes2
Notes2Notes2
Notes2
 
Introductionof c
Introductionof cIntroductionof c
Introductionof c
 
Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Acm aleppo cpc training ninth session
Acm aleppo cpc training ninth sessionAcm aleppo cpc training ninth session
Acm aleppo cpc training ninth session
 

Similar to Introduction to Python 2 (part 2/2)

Little book of programming challenges
Little book of programming challengesLittle book of programming challenges
Little book of programming challengesysolanki78
 
Py4Inf-05-Iterations-Print.pdf
Py4Inf-05-Iterations-Print.pdfPy4Inf-05-Iterations-Print.pdf
Py4Inf-05-Iterations-Print.pdfRupali420597
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)karan saini
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)karan saini
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterationskaran saini
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxAmy Nightingale
 
Lab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docx
Lab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docxLab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docx
Lab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docxABDULAHAD507571
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiProfessor Lili Saghafi
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3aRuth Marvin
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java ProgrammingPokequesthero
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdfCBJWorld
 

Similar to Introduction to Python 2 (part 2/2) (14)

Little book of programming challenges
Little book of programming challengesLittle book of programming challenges
Little book of programming challenges
 
Py4Inf-05-Iterations-Print.pdf
Py4Inf-05-Iterations-Print.pdfPy4Inf-05-Iterations-Print.pdf
Py4Inf-05-Iterations-Print.pdf
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterations
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
 
Lab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docx
Lab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docxLab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docx
Lab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docx
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3a
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
ForLoops.pptx
ForLoops.pptxForLoops.pptx
ForLoops.pptx
 
Java Practice Set
Java Practice SetJava Practice Set
Java Practice Set
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 

Recently uploaded

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 

Recently uploaded (20)

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

Introduction to Python 2 (part 2/2)

  • 1. INTRODUCTION TO PYTHON PART 2 OF 2 Silvia Tinena Coris Electronics & Automation Course Leader
  • 2. Silvia Tinena Coris Table of contents Part 1:  1. Learning Outcomes  2. Introduction  3. The first program: hello world!  4. The second program: hello (your name)!  5. More data types  6. Important things to know  7. If Statement  8. The third program: boy or girl?  9. For Statement  10. The fourth and fifth programs: your name + hello students! 2
  • 3. Silvia Tinena Coris Table of contents Part 2:  11. While Statement  12. The sixth program: security word  13. Functions  14. Seventh program: my functions  15. Interesting links  16. Exercises 3
  • 6. Silvia Tinena Coris While 6  The loop is executed as long as the condition is true  Operands for the condition:  < less than  > greater than  == equal to  <= less than or equal to  >= greater than or equal to  != different to while (True): … Infinite loop!
  • 7. Silvia Tinena Coris While 7 Now, try it yourself! Create your own loop with some operations on it
  • 8. 12. Sixth program: security word8
  • 9. Silvia Tinena Coris Security word – level 1 9
  • 10. Silvia Tinena Coris Security word – level 2 10  If the user has typed the security word wrong 5 times, then wait for 30 seconds before allowing the user to type it again.  To do so:  import time (at the very beginning of the program)  when you want the delay: time.sleep(s)  being ‘s’ the number of seconds you want the program to stop
  • 11. Silvia Tinena Coris Security word – level 3 11  If the user writes down the security word wrong 5 times, wait for 30 seconds.  The second time that the user writes 5 wrong security words, wait for 60 seconds.  The third time, wait for 90 seconds.  The fourth, 120 seconds.  And so on…
  • 12. Silvia Tinena Coris Security word – level 3 12
  • 13. Silvia Tinena Coris Security word – PRO 13  Now, we are going to emulate a login system from any website/computer  First, you need to ask the user for a login name
  • 15. Silvia Tinena Coris Functions 15  Useful for shortening the code and automatising sets of actions that will be repeatedly used together and in the same order def function_name(internal_name_input1, internal_name_input2): … print(output1, output2)  To call it: function_name(value_1st_input, value_2nd_input)
  • 16. Silvia Tinena Coris Functions 16 Now, try it yourself! Create your own function and call it as man times as you want with different input values
  • 17. 14. Seventh program: my functions 17
  • 18. Silvia Tinena Coris My functions 18  Create a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers.  Make sure that it works with any list of numbers, regardless of its length.  For that, create a couple of lists which have different lengths and try to call the function with each.
  • 19. Silvia Tinena Coris My functions – solutions 19 Now, try it yourself! Create your own list and use it in a function
  • 20. Silvia Tinena Coris My functions – solutions 20 Now, try it yourself! Create your own list and use it in a function
  • 22. Silvia Tinena Coris Interesting links 22  https://docs.python.org/3/  https://docs.python.org/2/reference/index.html  http://www.python- course.eu/sequential_data_types.php  http://www.tutorialspoint.com/python/
  • 23. Remember to do the flowchart and the pseudo code BEFORE programming!!! 16. Exercises23
  • 24. Silvia Tinena Coris 0: A, B, C, D or E? 24  Take the pseudo code of the exercise where the user is asked the result of an exam (0-100) and the program returns the equivalent mark in terms of A, B, C, D or E.
  • 25. Silvia Tinena Coris 0: A, B, C, D or E? – solution 25
  • 26. Silvia Tinena Coris 1: Histogram 26  Create a program which, given 7 inputs, writes down a histogram. (Note that some of the inputs can be 0)  eg. 2, 5, 6, 8, 10, 7, 11 ** ***** ****** ******** ********** ******* ***********
  • 27. Silvia Tinena Coris 1: Histogram – solution 27
  • 28. Silvia Tinena Coris 1: Histogram – solution 28
  • 29. Silvia Tinena Coris 1: Histogram – solution with for and list29
  • 30. Silvia Tinena Coris 2: 99 bottles of beer 30  Level 1:  Traditional song in the USA and Canada 99 bottles of beer on the wall, 99 bottles of beer. Take one down, pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take one down, pass it around, 97 bottles of beer on the wall. etc.  Create a Python program which generates all the verses of this song.
  • 31. Silvia Tinena Coris 2: 99 bottles of beer 31  Level 2:  Modify the last verse like this (underlined): 1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, no more bottles of beer on the wall. No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.
  • 32. Silvia Tinena Coris 2: 99 bottles of beer 32  Level 3:  If you have developed your code using range, now use while  If you have developed your code using while, now use range  Level 4:  Learn the song and sing it!
  • 33. Silvia Tinena Coris 2: 99 bottles of beer – solution 33  Using range
  • 34. Silvia Tinena Coris 2: 99 bottles of beer – solution 34  Using while
  • 35. Silvia Tinena Coris 2: 99 bottles of beer – solution 35
  • 36. Silvia Tinena Coris 3: age guess  Level 1: start with the basic functionality  Ask the year when the user was born  Display the age  Level 2:  Polish the program asking the user if he or she has already celebrated the birthday this year. This will mean a ± 1 year.  Level 3:  Greet the user and make a sentence with his or her name plus his or her age  eg. (name), you’re (age) years old! 36
  • 37. Silvia Tinena Coris 3: age guess  Level 4:  If the user has born this year, display a special message  eg. You are still a baby!  Level 5:  Avoid negative ages by displaying a message if the user enters a year superior to that we are in (2014)  eg. Oops! You aren’t born yet!  Level 6:  In the same situation as before, ask the user to come back in X years’ time, when he or she will be born  eg. Come back in (X) years’ time! 37
  • 38. Silvia Tinena Coris 3: age guess  Level 7:  Instead of pressing 1 or 2 to know if the birthday has been celebrated yet, ask the user to write yes or no (lowercase)  Level 8:  Create age blocks with personalised messages for each  1~12: You are a child!  13~19: You are a teenager!  20~30: You are a youngster!  31~65: You are an adult!  >65: You are a senior! 38
  • 39. Silvia Tinena Coris 3: age guess – solution 39  Levels 1 to 6
  • 40. Silvia Tinena Coris 3: age guess – solution 40  Levels 7 and 8
  • 41. Silvia Tinena Coris The random function with a list 41  1. Import the random module at the very first line of the program.  import random  2. Create a list of things to be randomly picked. This means that if you want a random animal, you will have to create a list of animals.  3. Assign to a variable a random value of the list.  my_fav_animal = random.choice(animals)  4. Now you can use my_fav_animal as usual!
  • 42. Silvia Tinena Coris The random function with a list 42 Now, try it yourself! Create your own list and randomly pick one of its items
  • 43. Silvia Tinena Coris The random function with numbers 43  1. Import the random module at the very first line of the program.  import random  2. Assign to a variable a random value of the defined range.  random_num = random.randint(1,100)  3. Now you can use your random_num as usual!
  • 44. Silvia Tinena Coris The random function with numbers 44 Now, try it yourself! Randomly pick one number from a defined range
  • 45. Silvia Tinena Coris 4: Story time 45  You are going to create a story which will be different every time you run the program  For that, we will create different lists and use the random function
  • 46. Silvia Tinena Coris 4: Story time  Ask the user his or her name and gender and store them in appropriate variables  Create a variable called ‘pronoun‘ which will obviously be he, she or it. Assign it depending on the gender  Create four lists:  Roles, names, actions and places  eg. roles = [‘knight’, ‘princess’, ‘prince’, ‘wizard’]  They can have as many items as you want and not all the list have to have the same number of words 46
  • 47. Silvia Tinena Coris 4: Story time  Now write the story, all in the same line!  Suggestion: write the story in a variable and then do a print of this variable  If you aren’t feeling imaginative, you can borrow this story: Once upon a time, there was a (role) called (user’s name). (pronoun) and some friends found themselves in the magic land of (place). This land was ruled by (name) the (role2). All of a sudden a mysterious voice spoke to them from high in the sky and said: “you must (action) (name) the (role2) to lift the curse of not being able to use technology…” 47
  • 48. Silvia Tinena Coris 4: Story time – solution 48
  • 49. Silvia Tinena Coris 5: Guess the number 49  Level 1:  The program will randomly generate a number between 1 and 100 and the user will have to guess it.  To help the user, the program will “say” whether the number to be guessed is higher or lower than that the user entered.  Level 2:  Interact with the user and display personalised messages with his or her name.
  • 50. Silvia Tinena Coris 5: Guess the number 50  Level 3:  Count how long does it take to the user to guess the number and display different messages depending if the user has guessed it in the first round or not.  Level 4:  If the number guessed by the user is really close to the actual number (let’s say it’s 5 or less units lower or higher), display a special message
  • 51. Silvia Tinena Coris 5: Guess the number – a bit of help 51  Level 4:  if users_num < random_num:  if users_num – random_num <= 5  print (‘You’re almost there! Just a little bit higher!’)  if users_num > random_num:  if users_num – random_num >= 5  print (‘You’re almost there! Just a little bit lower!’)
  • 52. Silvia Tinena Coris 6: Hangman 52  I guess you all know how to play hangman!  The user can only fail 5 times  Level 1:  Fixed word  The remaining guesses are just a number on the screen (not the actual hangman)  Level 2:  List of words, random choice of them
  • 53. Silvia Tinena Coris 6: Hangman 53  Level 3:  Display a hangman. You can do one like this: O _|_/ | / d b  Level 4:  Give the option that another user writes down the secret word! (If there is only one player, the program will choose the secret word. If there are two players, the first user will write the word down and the second will have to guess it).  Hint: after the 1st player has written down the word, introduce a lot of spaces so the 2nd player can’t see the word!
  • 54. Silvia Tinena Coris 6: Hangman – solution 54  Levels 1 to 3
  • 55. Silvia Tinena Coris 6: Hangman – solution 55  Level 4
  • 56. Any comments or questions? Drop me an email: Silvia.tinena.coris@gmail.com Or add me on LinkedIN: Silvia Tinena Coris Thanks! Hope you’ve learned 56