SlideShare a Scribd company logo
COMPUTER STUDIES
INTRODUCTION TO PYTHON PROGRAMMING
Python programming is pretty straight forward.
>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30
>>> 48565878 * 578453
28093077826734
>>> 2 ** 8
256
>>> 23 / 7
3.2857142857142856
>>> 23 // 7
3
>>> 23 % 7
2
>>> 2 + 2
4
>>> (5 - 1) * ((7 + 1) / (3 - 1))
16.0
Order of operation:
B (Brackets) E (Exponent) D (Division) M (Multiplication) A (Addition) S (Subtraction)
Comments:
# this is the first comment
spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside quotes."
Single line comment: # this is a single line comment
Block comments “”” this is a block comments that we must put in a 3 double quote
Like this”””
Basic Arithmetic Operations
Operators:
+: Addition
-: Subtraction
*: Multiplication
/: Division
%: Modulo (remainder). The remainder can be calculated with the % operator
**: Power
//: floor division: if both operands are of type int, floor division is performed and an int is
returned
Variables
Variable = 18
Variable1 = “Hello world!”
Types of variables:
 int: integer 2, 4, 20
 float: the ones with a fractional part or decimal (3.4, 4.9, 3.14,…)
 complex:
 bool: Boolean value (Yes/No; True/False; 0/1)
 str: string this is a strings of characters “I am a string of characters”
Data types: Numbers, Strings, Lists, Tuples, Dictionaries, Lists
Functions
In the context of programming, a function is a named sequence of statements that performs a
desired operation. This operation is specified in a function definition. In Python, the syntax for a
function definition is: print(), input(), round()
Built-in functions
Numbers
Using Python as a Calculator
tax = 12.5 / 100
price = 100.50
bill = price * tax
pay = round(bill, 2)
print(“Your final bil is: “,pay,”$”)
The function round () (like in excel) rounds the result pay into 2 decimal places
Strings
Besides numbers, Python can also manipulate strings, which can be expressed in several ways.
They can be enclosed in single quotes ('...') or double quotes ("...") with the same result [2].  can
be used to escape quotes:
>>> 'spam eggs' # single quotes
'spam eggs'
>>> 'doesn't' # use ' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> ""Yes," he said."
'"Yes," he said.'
>>> '"Isn't," she said.'
'"Isn't," she said.'
Concatenate strings
Prefix = “Py”
Word = Prefix + “thon”
Lists
Built-in function: len()
Manipulate the element in the list.
Index element in the list
The other way around, if we want to know the index of the element in the list we can use:
This program will return the index of the element “horse” in the list that is 2
Modify, append elements in the list
Let’s modify (replace) “bull” with “parrot”
Bull is replaced by parrot
Extend the list and append elements
If we extend the list the new element is added at the end of the list
How if we move the “bull” at the beginning of the list
Bull is moved at the beginning of the list
Bull is added again in the list
Assignment to slices is also possible, and this can even change the size of the list or clear it
entirely
It is possible to nest lists (create lists containing other lists)
In x= [a, n]
The first element of the list must be a in that case x[0] =”a” and the second element must be
x[1] = n
If we want to show the elements of the list in a it must be written as: x[0][0,1,2]
Instead for the element from n, it is: x[1][0,1,2,3]
Et voilà c’est fait!
The elements of X[0] = [a, b, c]
So from the figure above: X [0] [0] = a; X [0] [1] = b; X [0] [2] = c
The elements of X [1] = [1, 2, 3]
So from the figure above: X [1] [0] = 1; X [1] [1] = 2; X [1] [2] = 3
x [ a , n= ]
First element
X [0]
Second element
X [1]
[0] [1] [2]
[0] [1] [2]
Conditions and Conditional Statements
if, else, elif (Elseif)
<, >, <=, >=, ==, !=
# and, or, not, !=, ==
if 12!=13
print (“YES")
a = 33
t = 22i
if a == 33 and not (t ==22):
print ("TRUE")
else:
print("FALSE")
apples = input("How many apples?")
if apples >= 6:
print ("I have more apples: ")
elif apples < 6:
print ("I have not enough apples: ")
else:
print (" I have nothing: ")
apples = int(input("How many apples?: "))
if apples >= 6:
print ("I have more apples: ",str(apples))
elif apples < 6:
print ("I have not enough apples: ",str(apples))
else:
print (" I have nothing: ",str(apples))
It should say, “I have nothing”
We try to compare string and integer so we need to
convert it into integer using the function int ()
We need to refine the code as follow:
Other way of writing multiple conditions
The use of the operator “and” to combine two
conditions and concatenation
Task1
Write a piece of code to display whether a user inputs 0, 1 or more and display this.
Task2
Write a piece of code to test a mark and achievement by each student.
Mark Grade
91 A*
90 A+
80 A
70 B
60 C
50 D
40 E
30 F
20 Fail
Answer
Task1
Task2
What we have learned so far?
Difference between = and ==
Even though it is a mark the print () function will interpret the value as a string so it is necessary
to convert it using the function str()
All string must be in between “ ” (quotes)
We can concatenate 2 strings using +
While condition
while condition :
indentedBlock
To make things concrete and numerical, suppose the following: The tea starts at 115 degrees
Fahrenheit. You want it at 112 degrees. A chip of ice turns out to lower the temperature one
degree each time. You test the temperature each time, and also print out the temperature before
reducing the temperature. In Python you could write and run the code below, saved in example
program cool.py:
1
2
3
4
5
6
temperature = 115
while temperature > 112: # first while loop code
print(temperature)
temperature = temperature - 1
print('The tea is cool enough.')
I added a final line after the while loop to remind you that execution follows sequentially after a
loop completes.
If you play computer and follow the path of execution, you could generate the following table.
Remember, that each time you reach the end of the indented block after the while heading,
execution returns to the while heading for another test:
Line temperature Comment
1 115
2 115 > 112 is true, do loop
3 prints 115
4 114 115 - 1 is 114, loop back
2 114 > 112 is true, do loop
3 prints 114
4 113 114 - 1 is 113, loop back
2 113 > 112 is true, do loop
3 prints 113
4 112 113 - 1 is 112, loop back
2 112 > 112 is false, skip loop
6 prints that the tea is cool
Each time the end of the indented loop body is reached, execution returns to the while loop
heading for another test. When the test is finally false, execution jumps past the indented body of
the while loop to the next sequential statement.
Test yourself: Following the code. Figure out what is printed. :
i = 4
while i < 9:
print(i)
i = i+2
For Loops
'''The number of repetitions is specified by the user.'''
n = int(input('Enter the number of times to repeat: '))
for i in range(n):
print('This is repetitious!')
Extension
name = input('What is your name? ')
if name.endswith('Gumby'):
print ('Hello, Mr. Gumby')

More Related Content

What's hot

Block ciphers
Block ciphersBlock ciphers
Block ciphers
Sandeep Joshi
 
iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat Sheet
Samuel Lampa
 
The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 10 of 84The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 10 of 84
Mahmoud Samir Fayed
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
ssuserd6b1fd
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>
Svetlin Nakov
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4
Randa Elanwar
 
Python Modules, Packages and Libraries
Python Modules, Packages and LibrariesPython Modules, Packages and Libraries
Python Modules, Packages and Libraries
Venugopalavarma Raja
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
Emertxe Information Technologies Pvt Ltd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
ssuserd6b1fd
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
vikram mahendra
 
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCEFUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
sxw2k
 
Python Modules and Libraries
Python Modules and LibrariesPython Modules and Libraries
Python Modules and Libraries
Venugopalavarma Raja
 
The Ring programming language version 1.9 book - Part 25 of 210
The Ring programming language version 1.9 book - Part 25 of 210The Ring programming language version 1.9 book - Part 25 of 210
The Ring programming language version 1.9 book - Part 25 of 210
Mahmoud Samir Fayed
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
Giovanni Della Lunga
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
Gil Cohen
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
gekiaruj
 
[1062BPY12001] Data analysis with R / April 26
[1062BPY12001] Data analysis with R / April 26[1062BPY12001] Data analysis with R / April 26
[1062BPY12001] Data analysis with R / April 26
Kevin Chun-Hsien Hsu
 

What's hot (20)

Block ciphers
Block ciphersBlock ciphers
Block ciphers
 
iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat Sheet
 
The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 10 of 84The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 10 of 84
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 
Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>Java Foundations: Lists, ArrayList<T>
Java Foundations: Lists, ArrayList<T>
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4
 
Python Modules, Packages and Libraries
Python Modules, Packages and LibrariesPython Modules, Packages and Libraries
Python Modules, Packages and Libraries
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCEFUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3
 
Python Modules and Libraries
Python Modules and LibrariesPython Modules and Libraries
Python Modules and Libraries
 
The Ring programming language version 1.9 book - Part 25 of 210
The Ring programming language version 1.9 book - Part 25 of 210The Ring programming language version 1.9 book - Part 25 of 210
The Ring programming language version 1.9 book - Part 25 of 210
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
[1062BPY12001] Data analysis with R / April 26
[1062BPY12001] Data analysis with R / April 26[1062BPY12001] Data analysis with R / April 26
[1062BPY12001] Data analysis with R / April 26
 

Similar to Introduction to python programming

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
Data Handling
Data Handling Data Handling
Data Handling
bharath916489
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
Abdul Haseeb
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
gilpinleeanna
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
DURAIMURUGANM2
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
BAINIDA
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
Mahmoud Samir Fayed
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdf
HimoZZZ
 
Python Programming
Python Programming Python Programming
Python Programming
Sreedhar Chowdam
 
The Ring programming language version 1.5.2 book - Part 175 of 181
The Ring programming language version 1.5.2 book - Part 175 of 181The Ring programming language version 1.5.2 book - Part 175 of 181
The Ring programming language version 1.5.2 book - Part 175 of 181
Mahmoud Samir Fayed
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
AnishaJ7
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
SALU18
 
Python basics
Python basicsPython basics
Python basics
Himanshu Awasthi
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
agnesdcarey33086
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
Dan Bowen
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
Ragu Nathan
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
Chu An
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
UadAccount
 

Similar to Introduction to python programming (20)

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Data Handling
Data Handling Data Handling
Data Handling
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
 
1. control structures in the python.pptx
1. control structures in the python.pptx1. control structures in the python.pptx
1. control structures in the python.pptx
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdf
 
Python Programming
Python Programming Python Programming
Python Programming
 
The Ring programming language version 1.5.2 book - Part 175 of 181
The Ring programming language version 1.5.2 book - Part 175 of 181The Ring programming language version 1.5.2 book - Part 175 of 181
The Ring programming language version 1.5.2 book - Part 175 of 181
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
 
Python basics
Python basicsPython basics
Python basics
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
 
Matlab1
Matlab1Matlab1
Matlab1
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
 

More from Rakotoarison Louis Frederick

Uses of databases
Uses of databasesUses of databases
Uses of databases
Rakotoarison Louis Frederick
 
Orang tua perlu pahami makna pendidikan anak oleh
Orang tua perlu pahami makna pendidikan anak olehOrang tua perlu pahami makna pendidikan anak oleh
Orang tua perlu pahami makna pendidikan anak oleh
Rakotoarison Louis Frederick
 
Algorithm
AlgorithmAlgorithm
Programming basics
Programming basicsProgramming basics
Programming basics
Rakotoarison Louis Frederick
 
Algorithm
AlgorithmAlgorithm
Kata Kerja Operasional
Kata Kerja OperasionalKata Kerja Operasional
Kata Kerja Operasional
Rakotoarison Louis Frederick
 
Fitur fitur yang ada di audacity
Fitur fitur yang ada di audacityFitur fitur yang ada di audacity
Fitur fitur yang ada di audacity
Rakotoarison Louis Frederick
 
Moustaffa audacity-1
Moustaffa audacity-1Moustaffa audacity-1
Moustaffa audacity-1
Rakotoarison Louis Frederick
 

More from Rakotoarison Louis Frederick (10)

Uses of databases
Uses of databasesUses of databases
Uses of databases
 
Orang tua perlu pahami makna pendidikan anak oleh
Orang tua perlu pahami makna pendidikan anak olehOrang tua perlu pahami makna pendidikan anak oleh
Orang tua perlu pahami makna pendidikan anak oleh
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Kata Kerja Operasional
Kata Kerja OperasionalKata Kerja Operasional
Kata Kerja Operasional
 
Jiwa seni di cendekia leadership school
Jiwa seni di cendekia leadership schoolJiwa seni di cendekia leadership school
Jiwa seni di cendekia leadership school
 
Webdesign(tutorial)
Webdesign(tutorial)Webdesign(tutorial)
Webdesign(tutorial)
 
Fitur fitur yang ada di audacity
Fitur fitur yang ada di audacityFitur fitur yang ada di audacity
Fitur fitur yang ada di audacity
 
Moustaffa audacity-1
Moustaffa audacity-1Moustaffa audacity-1
Moustaffa audacity-1
 

Recently uploaded

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 

Recently uploaded (20)

Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 

Introduction to python programming

  • 1. COMPUTER STUDIES INTRODUCTION TO PYTHON PROGRAMMING Python programming is pretty straight forward. >>> 2 + 3 * 6 20 >>> (2 + 3) * 6 30 >>> 48565878 * 578453 28093077826734 >>> 2 ** 8 256 >>> 23 / 7 3.2857142857142856 >>> 23 // 7 3 >>> 23 % 7 2 >>> 2 + 2 4 >>> (5 - 1) * ((7 + 1) / (3 - 1)) 16.0 Order of operation: B (Brackets) E (Exponent) D (Division) M (Multiplication) A (Addition) S (Subtraction) Comments: # this is the first comment spam = 1 # and this is the second comment # ... and now a third! text = "# This is not a comment because it's inside quotes." Single line comment: # this is a single line comment Block comments “”” this is a block comments that we must put in a 3 double quote Like this”””
  • 2. Basic Arithmetic Operations Operators: +: Addition -: Subtraction *: Multiplication /: Division %: Modulo (remainder). The remainder can be calculated with the % operator **: Power //: floor division: if both operands are of type int, floor division is performed and an int is returned Variables Variable = 18 Variable1 = “Hello world!” Types of variables:  int: integer 2, 4, 20  float: the ones with a fractional part or decimal (3.4, 4.9, 3.14,…)  complex:  bool: Boolean value (Yes/No; True/False; 0/1)  str: string this is a strings of characters “I am a string of characters” Data types: Numbers, Strings, Lists, Tuples, Dictionaries, Lists Functions In the context of programming, a function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition. In Python, the syntax for a function definition is: print(), input(), round()
  • 3. Built-in functions Numbers Using Python as a Calculator tax = 12.5 / 100 price = 100.50 bill = price * tax pay = round(bill, 2) print(“Your final bil is: “,pay,”$”) The function round () (like in excel) rounds the result pay into 2 decimal places
  • 4. Strings Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes ('...') or double quotes ("...") with the same result [2]. can be used to escape quotes: >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn't' # use ' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," he said.' '"Yes," he said.' >>> ""Yes," he said." '"Yes," he said.' >>> '"Isn't," she said.' '"Isn't," she said.' Concatenate strings Prefix = “Py” Word = Prefix + “thon” Lists Built-in function: len()
  • 5. Manipulate the element in the list. Index element in the list The other way around, if we want to know the index of the element in the list we can use: This program will return the index of the element “horse” in the list that is 2 Modify, append elements in the list Let’s modify (replace) “bull” with “parrot” Bull is replaced by parrot
  • 6. Extend the list and append elements If we extend the list the new element is added at the end of the list How if we move the “bull” at the beginning of the list Bull is moved at the beginning of the list Bull is added again in the list
  • 7. Assignment to slices is also possible, and this can even change the size of the list or clear it entirely It is possible to nest lists (create lists containing other lists)
  • 8. In x= [a, n] The first element of the list must be a in that case x[0] =”a” and the second element must be x[1] = n If we want to show the elements of the list in a it must be written as: x[0][0,1,2] Instead for the element from n, it is: x[1][0,1,2,3] Et voilà c’est fait!
  • 9. The elements of X[0] = [a, b, c] So from the figure above: X [0] [0] = a; X [0] [1] = b; X [0] [2] = c The elements of X [1] = [1, 2, 3] So from the figure above: X [1] [0] = 1; X [1] [1] = 2; X [1] [2] = 3 x [ a , n= ] First element X [0] Second element X [1] [0] [1] [2] [0] [1] [2]
  • 10. Conditions and Conditional Statements if, else, elif (Elseif) <, >, <=, >=, ==, != # and, or, not, !=, == if 12!=13 print (“YES") a = 33 t = 22i if a == 33 and not (t ==22): print ("TRUE") else: print("FALSE") apples = input("How many apples?") if apples >= 6: print ("I have more apples: ") elif apples < 6: print ("I have not enough apples: ") else: print (" I have nothing: ") apples = int(input("How many apples?: ")) if apples >= 6: print ("I have more apples: ",str(apples)) elif apples < 6: print ("I have not enough apples: ",str(apples)) else: print (" I have nothing: ",str(apples)) It should say, “I have nothing” We try to compare string and integer so we need to convert it into integer using the function int ()
  • 11. We need to refine the code as follow: Other way of writing multiple conditions The use of the operator “and” to combine two conditions and concatenation
  • 12. Task1 Write a piece of code to display whether a user inputs 0, 1 or more and display this. Task2 Write a piece of code to test a mark and achievement by each student. Mark Grade 91 A* 90 A+ 80 A 70 B 60 C 50 D 40 E 30 F 20 Fail
  • 14.
  • 15. What we have learned so far? Difference between = and == Even though it is a mark the print () function will interpret the value as a string so it is necessary to convert it using the function str() All string must be in between “ ” (quotes) We can concatenate 2 strings using + While condition while condition : indentedBlock To make things concrete and numerical, suppose the following: The tea starts at 115 degrees Fahrenheit. You want it at 112 degrees. A chip of ice turns out to lower the temperature one degree each time. You test the temperature each time, and also print out the temperature before reducing the temperature. In Python you could write and run the code below, saved in example program cool.py: 1 2 3 4 5 6 temperature = 115 while temperature > 112: # first while loop code print(temperature) temperature = temperature - 1 print('The tea is cool enough.') I added a final line after the while loop to remind you that execution follows sequentially after a loop completes. If you play computer and follow the path of execution, you could generate the following table. Remember, that each time you reach the end of the indented block after the while heading, execution returns to the while heading for another test:
  • 16. Line temperature Comment 1 115 2 115 > 112 is true, do loop 3 prints 115 4 114 115 - 1 is 114, loop back 2 114 > 112 is true, do loop 3 prints 114 4 113 114 - 1 is 113, loop back 2 113 > 112 is true, do loop 3 prints 113 4 112 113 - 1 is 112, loop back 2 112 > 112 is false, skip loop 6 prints that the tea is cool Each time the end of the indented loop body is reached, execution returns to the while loop heading for another test. When the test is finally false, execution jumps past the indented body of the while loop to the next sequential statement. Test yourself: Following the code. Figure out what is printed. : i = 4 while i < 9: print(i) i = i+2 For Loops '''The number of repetitions is specified by the user.''' n = int(input('Enter the number of times to repeat: ')) for i in range(n): print('This is repetitious!')
  • 17. Extension name = input('What is your name? ') if name.endswith('Gumby'): print ('Hello, Mr. Gumby')