SlideShare a Scribd company logo
1 of 57
Download to read offline
INTRODUCTION TO PYTHON
PART 1 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
1. Learning Outcomes4
Silvia Tinena Coris
Learning Outcomes
 State the basis of Python programming
 Discover the data types
 Learn the different types of statements
 Do some simple programs
5
2. Introduction6
Silvia Tinena Coris
Why Python?
7
 Easy to learn
 Powerful programming language
 Efficient high-level data structures
 Simple but effective approach to object-
oriented programming
 Elegant syntax and dynamic typing
Silvia Tinena Coris
How to use Python?
8
 From the shell or from the saved programs.
 The shell executes each instruction as you type it
 Useful for experiments
 Each line starts with three chevron: >>>
 The saved programs are executed all at once.
 If something isn’t correct, an error line will appear
in red explaining which kind of mistake it is and
where it is
Silvia Tinena Coris
How to get Python?
 From a PC, downloadable for free in the
official website:
 https://www.python.org/downloads/
9
Silvia Tinena Coris
How to get Python?
 From Raspberry Pi, it is already downloaded
and ready to use in the desktop (IDLE or IDLE
3)
10
Silvia Tinena Coris
Comments
 They start with a hash character (#)
 They finish at the end of the line
11
Silvia Tinena Coris
Numbers
12
 int: signed integer numbers
 long: long integers (can also be represented in
octal or hex)
 float: real numbers, fractional part, floating point
 complex: complex numbers (j for the imaginary
part)
Silvia Tinena Coris
Numbers
13
 0x  indicates hexadecimal
 L, l  indicates long integer
 It is better to write an uppercase L to avoid
confusion with the number 1
3. The first program: hello world!14
Silvia Tinena Coris
First steps
 Open the IDLE
 File / New File
 Write your code on the new window!
 Save it
 Save before running the program (ctrl + s)
 If not, it will ask you to save it. Always say yes!
 To run: Run / Run Module or F5
 Be careful!!! Python is case
sensitive
 To interrupt the program: ctrl+c
15
Silvia Tinena Coris
Hello world!
16
Silvia Tinena Coris
Hello world!
17
4. The second program: hello
(your name)!
18
Silvia Tinena Coris
Hello (your name)!
19
Now, try it yourself!
Create a questionnaire and swap places with
your classmates to see who has the best one
5. More data types20
Silvia Tinena Coris
Strings
21
 Contiguous set of characters in between quotation marks,
which can be simple (‘ ‘) or double (“ “)
 Accessing a single positing of a string:
 String[n]
 Where n is the desired position
 Accessing more than one position in a string:
 String[0:2]
 Take the positions 0 and 1 of the string
 The first number is included
 The second isn’t!!
 Remember: the first position is included, the last isn’t
Silvia Tinena Coris
Strings
 Accessing more than one position in a string:
 String[:2]
 Take the first two positions
 If there is no number  0, from the beginning
 String[2:]
 Take from the second position to the end
 If there is no number  the end
 +  concatenation
 *  repetition
22
Silvia Tinena Coris
Strings
23
Now, try it yourself!
Create a string and play with it
Silvia Tinena Coris
Lists
24
 Enclosed within square brackets ([ ])
 Separated by commas (,)
 It is possible to have different types of data
within the same string
 They can be accessed as the strings
Silvia Tinena Coris
Lists
25
Now, try it yourself!
Create a list (or two) and
play with them
Silvia Tinena Coris
Tuples
26
 Similar to lists, but tuples CAN’T BE
MODIFIED
 “Only-read list”
 Enclosed within parentheses ( ( ) )
 Separated by commas
Silvia Tinena Coris
Tuples
27
Silvia Tinena Coris
Modifying Lists
28
Now, try it yourself!
Take the lists that you’ve just created
and modify some elements
Silvia Tinena Coris
Dictionary
29
 Enclosed within curly brackets ({ })
 Separated by commas (,)
 It is possible to have different types of data
within the same dictionary
 Function (variable) in (dictionary)
 It tells us whether the variable is in the dictionary
(True) or not (False)
Silvia Tinena Coris
Dictionary – level 1
30
Now, try it yourself!
Create your own shopping list
Silvia Tinena Coris
Dictionary – level 2
31
Now, try it yourself!
Create your own
personalised messages for
checking the items on your
shopping list
Silvia Tinena Coris
Dictionary – level 3
32
Now, try it
yourself!
Swap places with
your classmates
and see who
gets the highest
score
Silvia Tinena Coris
Converting types
33
int(x [,base])
Converts x to an integer. base specifies the base if x
is a string.
long(x [,base] )
Converts x to a long integer. base specifies the base
if x is a string.
float(x) Converts x to a floating-point number.
complex(real [,imag]) Creates a complex number.
str(x) Converts object x to a string representation.
tuple(s) Converts s to a tuple.
list(s) Converts s to a list.
hex(x) Converts an integer to a hexadecimal string.
oct(x) Converts an integer to an octal string.
Silvia Tinena Coris
Calculator
34
 +
 - will return the same type of number
 * if one is float, then the result will be float
 / will always return a float (decimals)
 // forces a floor division (no decimals) – div
 % calculates the remainder – mod
Silvia Tinena Coris
Calculator
35
Float
Integer
Now, try it
yourself!
Do some
Maths
directly on
the Shell
6. Important things to know36
Silvia Tinena Coris
Important things to know
37
 Declaration:=
 Used to define the value of a variable or a constant
 Comparison: ==
 Used to compare one variable or constant with a value
 It appears in statements such as if and while
 print (…) parentheses needed!
 len(string) it gives you the length of a string
 help(topic) it opens the help menu about the topic
 No need for semicolon at the end of the sentences!
 Key words in lowercase!
Silvia Tinena Coris
Important things to know
38
 Identifiers:
 Must start with a letter or an underscore
 Punctuation characters, @, $, £, %, etc. are not
allowed
 n
 It creates a line break
 You can write as many as you want together to create
more than one blank line.
 eg. nnn would create 3 blank lines
 True and False (Boolean) must have the first letter
upper-case! If so, they should be red (automatic)
Silvia Tinena Coris
Important things to know
39
 If we don’t want to end the program until the
user has pressed the enter key:
 input(‘Press the enter key to exit’)
 (Yes, it has to be the enter key)
 If you are printing strings and numbers in the
same line (the same print statement), you
should convert the numbers to string using the
function str()
 print(‘Your favourite number is
‘+str(favnumber))
Silvia Tinena Coris
Important things to know
40
 If we need that a string occupies more than
one line, start it with triple quotes (‘‘‘ ’’’) or (“““
”””)
 If a string has to contain an apostrophe (‘), it
should be written with a backslash before it to
avoid Python interpreting it as the end of the
string
 eg. print(‘They aren’t studying hard
enough.)
Silvia Tinena Coris
Coding Styles
 Use 4-space indentation, and no tabs.
 Wrap lines so that they don’t exceed 79
characters.
 Use blank lines to separate functions and classes,
and larger blocks of code inside functions.
 When possible, put comments on a line of their
own.
 Use spaces around operators and after commas,
but not directly inside bracketing constructs:
 a = f(1, 2) + g(3, 4)
41
Silvia Tinena Coris
Common Mistakes
42
 Double check that:
 The indentation of all loops is correct
 The blank spaces ( ) are where they should be
 Extra spaces may lead into errors
 All brackets are closed
 When editing the code, whichever is enclosed in the
brackets will be momentary highlighted in grey
 You have colons (:) after statement words
 The spelling of the variables, functions and
constants is the same throughout all the code
 Remember that Python is very sensitive!
7. If Statement43
Silvia Tinena Coris
if
44
if (condition 1):
Action 1
if (condition 1):
Action 1
else:
Action 2
if (condition 1):
Action 1
elif (condition 2):
Action 2
elif (condition 3):
Action 3
8. The third program: boy or girl?45
Silvia Tinena Coris
Boy or girl? – version 1
We need to convert the input to an integer!
46
Now, try it yourself!
Create your own personalised
messages and options
Silvia Tinena Coris
Boy or girl? – version 1
47
Silvia Tinena Coris
Boy or girl? – version 2
48
Now, try it yourself!
Create your own
personalised messages
Silvia Tinena Coris
Boy or girl? – version 2.2
49
Now, try it yourself!
Create your own personalised messages
with different options and different
questions
9. For and Range Statements50
Silvia Tinena Coris
For
51
 In other programming languages, for is used
to iterate a sequence over certain conditions
defined by the user… but not in Python!
 In Python, it iterates over the items of any
sequence (a list or a string), in the order that
they appear in the sequence.
for (i) in (string):
…
Silvia Tinena Coris
For
52
Now, try it yourself!
Create your own list and
iterate on it to print all of its
elements on separate lines
Why not trying adding
something after each word?
Silvia Tinena Coris
Range
53
 Is like the for function in other programming
languages
for (i) in range(n):
…
 Remember that Python works with 0 as
the first element! Now, try it
yourself!
10. Fourth and fifth programs:
your name + hello students
54
Silvia Tinena Coris
Your name in different lines
55
Now, try it yourself!
Create your own question
and iterate it
Silvia Tinena Coris
Hello students
56
Now, try it yourself!
Create your own list and
iterate on it to print all of its
elements on separate lines
with a message
Now go to Part 257

More Related Content

What's hot

programming with python ppt
programming with python pptprogramming with python ppt
programming with python pptPriyanka Pradhan
 
Programming in Python
Programming in Python Programming in Python
Programming in Python Tiji Thomas
 
Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasserySHAMJITH KM
 
Introduction to Python Language and Data Types
Introduction to Python Language and Data TypesIntroduction to Python Language and Data Types
Introduction to Python Language and Data TypesRavi Shankar
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming LanguageRohan Gupta
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginnersKálmán "KAMI" Szalai
 
Python introduction
Python introductionPython introduction
Python introductionleela rani
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Fariz Darari
 

What's hot (20)

programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
 
Programming in Python
Programming in Python Programming in Python
Programming in Python
 
Python programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - KalamasseryPython programming Workshop SITTTR - Kalamassery
Python programming Workshop SITTTR - Kalamassery
 
Introduction to Python Language and Data Types
Introduction to Python Language and Data TypesIntroduction to Python Language and Data Types
Introduction to Python Language and Data Types
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
Python Basics
Python Basics Python Basics
Python Basics
 
While loop
While loopWhile loop
While loop
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginners
 
Python introduction
Python introductionPython introduction
Python introduction
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python basic
Python basicPython basic
Python basic
 
Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02Basic Python Programming: Part 01 and Part 02
Basic Python Programming: Part 01 and Part 02
 
python.ppt
python.pptpython.ppt
python.ppt
 
Python
PythonPython
Python
 
Python ppt
Python pptPython ppt
Python ppt
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 

Similar to Introduction to Python (part 1/2)

Python-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptxPython-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptxElijahSantos4
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine LearningYounesCharfaoui
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxpriestmanmable
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxadihartanto7
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second sessionAhmad Bashar Eter
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2Abdul Haseeb
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1Nicholas I
 
STRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptxSTRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptxTinku91
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/elseMarc Gouw
 
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
 
Python quick guide
Python quick guidePython quick guide
Python quick guideHasan Bisri
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 

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

Python-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptxPython-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptx
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine Learning
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Introduction to Computing
Introduction to ComputingIntroduction to Computing
Introduction to Computing
 
Looping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptxLooping in PythonLab8 lecture slides.pptx
Looping in PythonLab8 lecture slides.pptx
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Chapter08.pptx
Chapter08.pptxChapter08.pptx
Chapter08.pptx
 
Python
PythonPython
Python
 
python-ch2.pptx
python-ch2.pptxpython-ch2.pptx
python-ch2.pptx
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1
 
Introductionof c
Introductionof cIntroductionof c
Introductionof c
 
M C6java2
M C6java2M C6java2
M C6java2
 
STRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptxSTRINGS_IN_PYTHON 9-12 (1).pptx
STRINGS_IN_PYTHON 9-12 (1).pptx
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/else
 
Python programming
Python  programmingPython  programming
Python programming
 
Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)Coding in Disaster Relief - Worksheet (Advanced)
Coding in Disaster Relief - Worksheet (Advanced)
 
Python quick guide
Python quick guidePython quick guide
Python quick guide
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 

Recently uploaded

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
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.
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 

Recently uploaded (20)

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
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...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 

Introduction to Python (part 1/2)

  • 1. INTRODUCTION TO PYTHON PART 1 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
  • 5. Silvia Tinena Coris Learning Outcomes  State the basis of Python programming  Discover the data types  Learn the different types of statements  Do some simple programs 5
  • 7. Silvia Tinena Coris Why Python? 7  Easy to learn  Powerful programming language  Efficient high-level data structures  Simple but effective approach to object- oriented programming  Elegant syntax and dynamic typing
  • 8. Silvia Tinena Coris How to use Python? 8  From the shell or from the saved programs.  The shell executes each instruction as you type it  Useful for experiments  Each line starts with three chevron: >>>  The saved programs are executed all at once.  If something isn’t correct, an error line will appear in red explaining which kind of mistake it is and where it is
  • 9. Silvia Tinena Coris How to get Python?  From a PC, downloadable for free in the official website:  https://www.python.org/downloads/ 9
  • 10. Silvia Tinena Coris How to get Python?  From Raspberry Pi, it is already downloaded and ready to use in the desktop (IDLE or IDLE 3) 10
  • 11. Silvia Tinena Coris Comments  They start with a hash character (#)  They finish at the end of the line 11
  • 12. Silvia Tinena Coris Numbers 12  int: signed integer numbers  long: long integers (can also be represented in octal or hex)  float: real numbers, fractional part, floating point  complex: complex numbers (j for the imaginary part)
  • 13. Silvia Tinena Coris Numbers 13  0x  indicates hexadecimal  L, l  indicates long integer  It is better to write an uppercase L to avoid confusion with the number 1
  • 14. 3. The first program: hello world!14
  • 15. Silvia Tinena Coris First steps  Open the IDLE  File / New File  Write your code on the new window!  Save it  Save before running the program (ctrl + s)  If not, it will ask you to save it. Always say yes!  To run: Run / Run Module or F5  Be careful!!! Python is case sensitive  To interrupt the program: ctrl+c 15
  • 18. 4. The second program: hello (your name)! 18
  • 19. Silvia Tinena Coris Hello (your name)! 19 Now, try it yourself! Create a questionnaire and swap places with your classmates to see who has the best one
  • 20. 5. More data types20
  • 21. Silvia Tinena Coris Strings 21  Contiguous set of characters in between quotation marks, which can be simple (‘ ‘) or double (“ “)  Accessing a single positing of a string:  String[n]  Where n is the desired position  Accessing more than one position in a string:  String[0:2]  Take the positions 0 and 1 of the string  The first number is included  The second isn’t!!  Remember: the first position is included, the last isn’t
  • 22. Silvia Tinena Coris Strings  Accessing more than one position in a string:  String[:2]  Take the first two positions  If there is no number  0, from the beginning  String[2:]  Take from the second position to the end  If there is no number  the end  +  concatenation  *  repetition 22
  • 23. Silvia Tinena Coris Strings 23 Now, try it yourself! Create a string and play with it
  • 24. Silvia Tinena Coris Lists 24  Enclosed within square brackets ([ ])  Separated by commas (,)  It is possible to have different types of data within the same string  They can be accessed as the strings
  • 25. Silvia Tinena Coris Lists 25 Now, try it yourself! Create a list (or two) and play with them
  • 26. Silvia Tinena Coris Tuples 26  Similar to lists, but tuples CAN’T BE MODIFIED  “Only-read list”  Enclosed within parentheses ( ( ) )  Separated by commas
  • 28. Silvia Tinena Coris Modifying Lists 28 Now, try it yourself! Take the lists that you’ve just created and modify some elements
  • 29. Silvia Tinena Coris Dictionary 29  Enclosed within curly brackets ({ })  Separated by commas (,)  It is possible to have different types of data within the same dictionary  Function (variable) in (dictionary)  It tells us whether the variable is in the dictionary (True) or not (False)
  • 30. Silvia Tinena Coris Dictionary – level 1 30 Now, try it yourself! Create your own shopping list
  • 31. Silvia Tinena Coris Dictionary – level 2 31 Now, try it yourself! Create your own personalised messages for checking the items on your shopping list
  • 32. Silvia Tinena Coris Dictionary – level 3 32 Now, try it yourself! Swap places with your classmates and see who gets the highest score
  • 33. Silvia Tinena Coris Converting types 33 int(x [,base]) Converts x to an integer. base specifies the base if x is a string. long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string. float(x) Converts x to a floating-point number. complex(real [,imag]) Creates a complex number. str(x) Converts object x to a string representation. tuple(s) Converts s to a tuple. list(s) Converts s to a list. hex(x) Converts an integer to a hexadecimal string. oct(x) Converts an integer to an octal string.
  • 34. Silvia Tinena Coris Calculator 34  +  - will return the same type of number  * if one is float, then the result will be float  / will always return a float (decimals)  // forces a floor division (no decimals) – div  % calculates the remainder – mod
  • 35. Silvia Tinena Coris Calculator 35 Float Integer Now, try it yourself! Do some Maths directly on the Shell
  • 36. 6. Important things to know36
  • 37. Silvia Tinena Coris Important things to know 37  Declaration:=  Used to define the value of a variable or a constant  Comparison: ==  Used to compare one variable or constant with a value  It appears in statements such as if and while  print (…) parentheses needed!  len(string) it gives you the length of a string  help(topic) it opens the help menu about the topic  No need for semicolon at the end of the sentences!  Key words in lowercase!
  • 38. Silvia Tinena Coris Important things to know 38  Identifiers:  Must start with a letter or an underscore  Punctuation characters, @, $, £, %, etc. are not allowed  n  It creates a line break  You can write as many as you want together to create more than one blank line.  eg. nnn would create 3 blank lines  True and False (Boolean) must have the first letter upper-case! If so, they should be red (automatic)
  • 39. Silvia Tinena Coris Important things to know 39  If we don’t want to end the program until the user has pressed the enter key:  input(‘Press the enter key to exit’)  (Yes, it has to be the enter key)  If you are printing strings and numbers in the same line (the same print statement), you should convert the numbers to string using the function str()  print(‘Your favourite number is ‘+str(favnumber))
  • 40. Silvia Tinena Coris Important things to know 40  If we need that a string occupies more than one line, start it with triple quotes (‘‘‘ ’’’) or (“““ ”””)  If a string has to contain an apostrophe (‘), it should be written with a backslash before it to avoid Python interpreting it as the end of the string  eg. print(‘They aren’t studying hard enough.)
  • 41. Silvia Tinena Coris Coding Styles  Use 4-space indentation, and no tabs.  Wrap lines so that they don’t exceed 79 characters.  Use blank lines to separate functions and classes, and larger blocks of code inside functions.  When possible, put comments on a line of their own.  Use spaces around operators and after commas, but not directly inside bracketing constructs:  a = f(1, 2) + g(3, 4) 41
  • 42. Silvia Tinena Coris Common Mistakes 42  Double check that:  The indentation of all loops is correct  The blank spaces ( ) are where they should be  Extra spaces may lead into errors  All brackets are closed  When editing the code, whichever is enclosed in the brackets will be momentary highlighted in grey  You have colons (:) after statement words  The spelling of the variables, functions and constants is the same throughout all the code  Remember that Python is very sensitive!
  • 44. Silvia Tinena Coris if 44 if (condition 1): Action 1 if (condition 1): Action 1 else: Action 2 if (condition 1): Action 1 elif (condition 2): Action 2 elif (condition 3): Action 3
  • 45. 8. The third program: boy or girl?45
  • 46. Silvia Tinena Coris Boy or girl? – version 1 We need to convert the input to an integer! 46 Now, try it yourself! Create your own personalised messages and options
  • 47. Silvia Tinena Coris Boy or girl? – version 1 47
  • 48. Silvia Tinena Coris Boy or girl? – version 2 48 Now, try it yourself! Create your own personalised messages
  • 49. Silvia Tinena Coris Boy or girl? – version 2.2 49 Now, try it yourself! Create your own personalised messages with different options and different questions
  • 50. 9. For and Range Statements50
  • 51. Silvia Tinena Coris For 51  In other programming languages, for is used to iterate a sequence over certain conditions defined by the user… but not in Python!  In Python, it iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. for (i) in (string): …
  • 52. Silvia Tinena Coris For 52 Now, try it yourself! Create your own list and iterate on it to print all of its elements on separate lines Why not trying adding something after each word?
  • 53. Silvia Tinena Coris Range 53  Is like the for function in other programming languages for (i) in range(n): …  Remember that Python works with 0 as the first element! Now, try it yourself!
  • 54. 10. Fourth and fifth programs: your name + hello students 54
  • 55. Silvia Tinena Coris Your name in different lines 55 Now, try it yourself! Create your own question and iterate it
  • 56. Silvia Tinena Coris Hello students 56 Now, try it yourself! Create your own list and iterate on it to print all of its elements on separate lines with a message
  • 57. Now go to Part 257