Python tutorial

370 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
370
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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 n in range(2, 10): 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. Python Lists pets = [2, 'dogs', ['and', 'one', 'cat']] pets[1] >>> dogs pets[2] >>> ['and', 'one', 'cat']

×