python tutorial

Post on 03-Sep-2014

329 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

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 = 4pi = 3.14area = 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 Statementfor 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 redred redred 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] >>> dogspets[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 ofstrings where the string length is 2 or more and the firstand last chars of the string are the same.

3 . Given a list of strings, return a list with the stringsin 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')]

Exercises1. given a string "abbabcbdbabdbdbabababcbcbab", construct a dictionary containing letter frequency in the string.

top related