SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
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')

Contenu connexe

Tendances

iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetSamuel 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 84Mahmoud 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 PYTHONvikram 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 4Randa Elanwar
 
Python Modules, Packages and Libraries
Python Modules, Packages and LibrariesPython Modules, Packages and Libraries
Python Modules, Packages and LibrariesVenugopalavarma Raja
 
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-PART1vikram 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 SCIENCEVenugopalavarma Raja
 
Cheat sheet python3
Cheat sheet python3Cheat sheet python3
Cheat sheet python3sxw2k
 
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 210Mahmoud 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 1Giovanni Della Lunga
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil 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 26Kevin Chun-Hsien Hsu
 

Tendances (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
 

Similaire à Introduction to python programming

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2Mohamed Ahmed
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul 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.docxgilpinleeanna
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshopBAINIDA
 
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 202Mahmoud Samir Fayed
 
Python cheatsheat.pdf
Python cheatsheat.pdfPython cheatsheat.pdf
Python cheatsheat.pdfHimoZZZ
 
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 181Mahmoud 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.pptAnishaJ7
 
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.docxSALU18
 
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).docxagnesdcarey33086
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schoolsDan Bowen
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 introRagu Nathan
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUadAccount
 

Similaire à 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
 
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
 
bobok
bobokbobok
bobok
 

Plus de 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
 

Dernier

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 

Dernier (20)

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 

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')