16
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 tutorial

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Python tutorial

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;

Page 2: Python tutorial

Python Basics

- readable, flexible code- lose the compile-time type checking in the source code; higher productivity- code is checked at runtime

Page 3: Python tutorial

Python Interpreter

- good for learning the language- good for experimenting with the library- helpful functions: dir(), help()

Page 4: Python tutorial

Python Variables

radius = 4pi = 3.14area = pi * radius * radius

Page 5: Python tutorial

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]

Page 6: Python tutorial

Python Indentation

def fib(n): print 'n=', n if n > 1: return n * fib(n-1) else: print 'end of line' return 1

Page 7: Python tutorial

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?”

Page 8: Python tutorial

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

Page 9: Python tutorial

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'

Page 10: Python tutorial

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.

Page 11: Python tutorial

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']

Page 12: Python tutorial

Python Tuples- tuples are immutable- fixed size

a = (1, 2, 3)

Page 13: Python tutorial

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']

Page 14: Python tutorial

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

Page 15: Python tutorial

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')]

Page 16: Python tutorial

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