Python Basics
- python is a dynamic, interpreted, object
oriented programming language
- source code does not declare the ...
Python Basics
- readable, flexible code
- lose the compile-time type checking in
the source code; higher productivity
- co...
Python Interpreter
- good for learning the language
- good for experimenting with the library
- helpful functions: dir(), ...
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 P...
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 “:” an...
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]...
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 ...
Exercises:
1. Write a program that asks two people for their names;
stores the names in variables; says hello to both of t...
Python Lists
pets = [2, 'dogs', ['and', 'one', 'cat']]
pets[1] >>> dogs
pets[2] >>> ['and', 'one', 'cat']
a = [4, 1, 2, 6]...
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 whet...
Python Dictionaries
- also known as associative arrays or
hash tables
- dictionaries consist of pairs of keys and
their co...
Python Dictionaries
d = {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}
d['o'] >>> 'omega'
d['b']
d.get('b')
d.keys() >>> ['a',...
Exercises
1. given a string "abbabcbdbabdbdbabababcbcbab",
construct a dictionary containing letter frequency in the
strin...
Upcoming SlideShare
Loading in …5
×

Python tutorial

326 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Python tutorial

  1. 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. 2. Python Basics - readable, flexible code - lose the compile-time type checking in the source code; higher productivity - code is checked at runtime
  3. 3. Python Interpreter - good for learning the language - good for experimenting with the library - helpful functions: dir(), help()
  4. 4. Python Variables radius = 4 pi = 3.14 area = pi * radius * radius
  5. 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. 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. 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. 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. 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. 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. 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. 12. Python Tuples - tuples are immutable - fixed size a = (1, 2, 3)
  13. 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. 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. 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. 16. Exercises 1. given a string "abbabcbdbabdbdbabababcbcbab", construct a dictionary containing letter frequency in the string.

×