SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
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.

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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
 

Similar a Python tutorial

Similar a 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
 

Último

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
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?Igalia
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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 TerraformAndrey Devyatkin
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
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 DevelopmentsTrustArc
 
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...apidays
 
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 MilvusZilliz
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
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 connectorsNanddeep Nachan
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
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 WorkerThousandEyes
 

Último (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
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 - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
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...
 
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
 
+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...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
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
 

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.