SlideShare une entreprise Scribd logo
1  sur  16
Télécharger pour lire hors ligne
Python Basics
- python is a dynamic, interpreted, object
oriented programming language
- source code does not declare the types
of variables, or parameters or methods
Static: Dynamic:
static int a_number; a_number = 42
a_number = 42;
Python Basics
- readable, flexible code
- lose the compile-time type checking in
the source code; higher productivity
- code is checked at runtime
Python Interpreter
- good for learning the language
- good for experimenting with the library
- helpful functions: dir(), help()
Python Variables
radius = 4
pi = 3.14
area = pi * radius * radius
Python Strings
- python string are immutable
spell = 'abrakadabra'
len(spell) >>> 11
a = Python
'Hello %s' %a >>> 'Hello Python'
a.lower() >>> 'python'
a.find('t') >>> 2
a[start:end]
Python Indentation
def fib(n):
print 'n=', n
if n > 1:
return n * fib(n-1)
else:
print 'end of line'
return 1
Python If Statement
- python does not use { } to enclose blocks of code for
if/loops/function etc.
- uses the colon “:” and indentation/whitespace to
group statements
- '==' is overloaded to work correctly with strings
if speed > 80:
print 'License and registration please'
if mood == 'terrible' or speed >= 100:
print 'You have the right to remain silent'
elif mood == 'bad' or speed >=90:
print “I'm going to have to give you a ticket”
else:
print “Let's keep it under 80, ok?”
Python For Statement
for x in range(5):
print x
for x in xrange(10):
if x % 2 == 0:
continue
print x
primes = [2, 3, 5, 7]
for prime in primes:
print prime
Python For Statement
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
print n, 'is a prime number'
Exercises:
1. Write a program that asks two people for their names;
stores the names in variables; says hello to both of them.
Use "raw_input". Don't use "+" for string concatenation.
2. Write a program that asks users for their favorite color.
Create the following output (assuming "red" is the chosen
color).
red red red red red red red red red red
red red
red red
red red red red red red red red red red
3 .print all multiples of 13 that are smaller than 100.
Python Lists
pets = [2, 'dogs', ['and', 'one', 'cat']]
pets[1] >>> dogs
pets[2] >>> ['and', 'one', 'cat']
a = [4, 1, 2, 6]
sorted(a) >>> [1, 2, 4, 6]
a = ['aaaz', 'cc', 'd', 'bbb']
b = ':'.join(a) >>> 'aaaz:cc:d:bbb'
b.split(':') >>> ['aaaz', 'cc', 'd', 'bbb']
Python Tuples
- tuples are immutable
- fixed size
a = (1, 2, 3)
Exercises
1. Create a list that contains the names of 5 students.
Create a for loop that asks the user for every name whether
they would like to keep the name or delete it. Delete the
names which the user no longer wants
2. Given a list of strings, return the count of the number of
strings where the string length is 2 or more and the first
and last chars of the string are the same.
3 . Given a list of strings, return a list with the strings
in sorted order, except group all the strings that begin with
'x' first.
eg. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] >>>
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
Python Dictionaries
- also known as associative arrays or
hash tables
- dictionaries consist of pairs of keys and
their corresponding values.
- strings, numbers, and tuples work as
keys, and any type can be a value
Python Dictionaries
d = {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}
d['o'] >>> 'omega'
d['b']
d.get('b')
d.keys() >>> ['a', 'g', 'o']
d.values() >>> ['alpha', 'gamma', 'omega']
d.items() >>> [('a', 'alpha'), ('g', 'gamma'), ('o', 'omega')]
Exercises
1. given a string "abbabcbdbabdbdbabababcbcbab",
construct a dictionary containing letter frequency in the
string.

Contenu connexe

Tendances

Tendances (18)

Python
PythonPython
Python
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc cs
 
Sequence Types in Python Programming
Sequence Types in Python ProgrammingSequence Types in Python Programming
Sequence Types in Python Programming
 
Python :variable types
Python :variable typesPython :variable types
Python :variable types
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Pythonintroduction
PythonintroductionPythonintroduction
Pythonintroduction
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Basics of Python
Basics of PythonBasics of Python
Basics of Python
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 
Iteration
IterationIteration
Iteration
 
Python course Day 1
Python course Day 1Python course Day 1
Python course Day 1
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 

Similaire à Python tutorial

Similaire à Python tutorial (20)

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 basic
Python basicPython basic
Python basic
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdf
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
Introduction to Python Basics
Introduction to Python BasicsIntroduction to Python Basics
Introduction to Python Basics
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
Python basics
Python basicsPython basics
Python basics
 
Ry pyconjp2015 turtle
Ry pyconjp2015 turtleRy pyconjp2015 turtle
Ry pyconjp2015 turtle
 
Python
PythonPython
Python
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Python tutorial

  • 1. Python Basics - python is a dynamic, interpreted, object oriented programming language - source code does not declare the types of variables, or parameters or methods Static: Dynamic: static int a_number; a_number = 42 a_number = 42;
  • 2. Python Basics - readable, flexible code - lose the compile-time type checking in the source code; higher productivity - code is checked at runtime
  • 3. Python Interpreter - good for learning the language - good for experimenting with the library - helpful functions: dir(), help()
  • 4. Python Variables radius = 4 pi = 3.14 area = pi * radius * radius
  • 5. Python Strings - python string are immutable spell = 'abrakadabra' len(spell) >>> 11 a = Python 'Hello %s' %a >>> 'Hello Python' a.lower() >>> 'python' a.find('t') >>> 2 a[start:end]
  • 6. Python Indentation def fib(n): print 'n=', n if n > 1: return n * fib(n-1) else: print 'end of line' return 1
  • 7. Python If Statement - python does not use { } to enclose blocks of code for if/loops/function etc. - uses the colon “:” and indentation/whitespace to group statements - '==' is overloaded to work correctly with strings if speed > 80: print 'License and registration please' if mood == 'terrible' or speed >= 100: print 'You have the right to remain silent' elif mood == 'bad' or speed >=90: print “I'm going to have to give you a ticket” else: print “Let's keep it under 80, ok?”
  • 8. Python For Statement for x in range(5): print x for x in xrange(10): if x % 2 == 0: continue print x primes = [2, 3, 5, 7] for prime in primes: print prime
  • 9. Python For Statement for x in range(2, n): if n % x == 0: print n, 'equals', x, '*', n/x break else: print n, 'is a prime number'
  • 10. Exercises: 1. Write a program that asks two people for their names; stores the names in variables; says hello to both of them. Use "raw_input". Don't use "+" for string concatenation. 2. Write a program that asks users for their favorite color. Create the following output (assuming "red" is the chosen color). red red red red red red red red red red red red red red red red red red red red red red red red 3 .print all multiples of 13 that are smaller than 100.
  • 11. Python Lists pets = [2, 'dogs', ['and', 'one', 'cat']] pets[1] >>> dogs pets[2] >>> ['and', 'one', 'cat'] a = [4, 1, 2, 6] sorted(a) >>> [1, 2, 4, 6] a = ['aaaz', 'cc', 'd', 'bbb'] b = ':'.join(a) >>> 'aaaz:cc:d:bbb' b.split(':') >>> ['aaaz', 'cc', 'd', 'bbb']
  • 12. Python Tuples - tuples are immutable - fixed size a = (1, 2, 3)
  • 13. Exercises 1. Create a list that contains the names of 5 students. Create a for loop that asks the user for every name whether they would like to keep the name or delete it. Delete the names which the user no longer wants 2. Given a list of strings, return the count of the number of strings where the string length is 2 or more and the first and last chars of the string are the same. 3 . Given a list of strings, return a list with the strings in sorted order, except group all the strings that begin with 'x' first. eg. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] >>> ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
  • 14. Python Dictionaries - also known as associative arrays or hash tables - dictionaries consist of pairs of keys and their corresponding values. - strings, numbers, and tuples work as keys, and any type can be a value
  • 15. Python Dictionaries d = {'a': 'alpha', 'o': 'omega', 'g': 'gamma'} d['o'] >>> 'omega' d['b'] d.get('b') d.keys() >>> ['a', 'g', 'o'] d.values() >>> ['alpha', 'gamma', 'omega'] d.items() >>> [('a', 'alpha'), ('g', 'gamma'), ('o', 'omega')]
  • 16. Exercises 1. given a string "abbabcbdbabdbdbabababcbcbab", construct a dictionary containing letter frequency in the string.