Difference between revisions of "Python functions"
From MyWiki
Line 28: | Line 28: | ||
a = add_numbers | a = add_numbers | ||
a(1,2) | a(1,2) | ||
+ | </source> | ||
+ | |||
+ | Types and sequences | ||
+ | <source lang="python"> | ||
+ | type('This is a string') | ||
+ | type(None) | ||
+ | type(1) | ||
+ | type(1.0) | ||
+ | type(add_numbers) | ||
+ | |||
+ | x = (1, 'a', 2, 'b') | ||
+ | type x tuples are immutable wheras lists are mutable | ||
+ | x = [1, 'a', 2, 'b'] list | ||
+ | x.append(3.3) | ||
+ | |||
+ | for item in x: | ||
+ | print(item) | ||
+ | OR | ||
+ | i=0 | ||
+ | while( i != len(x) ): | ||
+ | print(x[i]) | ||
+ | i = i + 1 | ||
+ | |||
+ | [1,2] + [3,4] | ||
+ | |||
+ | [1]*3 | ||
+ | |||
+ | 1 in [1, 2, 3] | ||
+ | |||
+ | x[-4:-2] | ||
+ | |||
+ | firstname = 'Christopher' | ||
+ | lastname = 'Brooks' | ||
+ | |||
+ | print(firstname + ' ' + lastname) | ||
+ | print(firstname*3) | ||
+ | print('Chris' in firstname) | ||
+ | |||
+ | firstname = 'Christopher Arthur Hansen Brooks'.split(' ')[0] # [0] selects the first element of the list | ||
+ | lastname = 'Christopher Arthur Hansen Brooks'.split(' ')[-1] # [-1] selects the last element of the list | ||
+ | print(firstname) | ||
+ | print(lastname) | ||
+ | |||
+ | 'Chris' + 2 | ||
+ | 'Chris' + str(2) | ||
+ | |||
+ | x = {'Christopher Brooks': 'brooksch@umich.edu', 'Bill Gates': 'billg@microsoft.com'} | ||
+ | x['Christopher Brooks'] # Retrieve a value by using the indexing operator | ||
+ | |||
+ | x['Kevyn Collins-Thompson'] = None | ||
+ | x['Kevyn Collins-Thompson'] | ||
+ | |||
+ | for name in x: | ||
+ | print(x[name]) | ||
+ | |||
+ | for email in x.values(): | ||
+ | print(email) | ||
+ | |||
+ | for name, email in x.items(): | ||
+ | print(name) | ||
+ | print(email) | ||
+ | |||
+ | x = ('Christopher', 'Brooks', 'brooksch@umich.edu') | ||
+ | fname, lname, email = x | ||
+ | |||
+ | x = ('Christopher', 'Brooks', 'brooksch@umich.edu', 'Ann Arbor') | ||
+ | fname, lname, email = x | ||
+ | |||
+ | more on strings | ||
+ | |||
+ | sales_record = { | ||
+ | 'price': 3.24, | ||
+ | 'num_items': 4, | ||
+ | 'person': 'Chris'} | ||
+ | |||
+ | sales_statement = '{} bought {} item(s) at a price of {} each for a total of {}' | ||
+ | |||
+ | print(sales_statement.format(sales_record['person'], | ||
+ | sales_record['num_items'], | ||
+ | sales_record['price'], | ||
+ | sales_r | ||
</source> | </source> |
Revision as of 09:41, 28 July 2019
def add_numbers(x,y,z=None): if (z==None): return x+y else: return x+y+z print(add_numbers(1, 2)) print(add_numbers(1, 2, 3))
def add_numbers(x, y, z=None, flag=False): if (flag): print('Flag is true!') if (z==None): return x + y else: return x + y + z print(add_numbers(1, 2, flag=True))
def add_numbers(x,y): return x+y a = add_numbers a(1,2)
Types and sequences
type('This is a string') type(None) type(1) type(1.0) type(add_numbers) x = (1, 'a', 2, 'b') type x tuples are immutable wheras lists are mutable x = [1, 'a', 2, 'b'] list x.append(3.3) for item in x: print(item) OR i=0 while( i != len(x) ): print(x[i]) i = i + 1 [1,2] + [3,4] [1]*3 1 in [1, 2, 3] x[-4:-2] firstname = 'Christopher' lastname = 'Brooks' print(firstname + ' ' + lastname) print(firstname*3) print('Chris' in firstname) firstname = 'Christopher Arthur Hansen Brooks'.split(' ')[0] # [0] selects the first element of the list lastname = 'Christopher Arthur Hansen Brooks'.split(' ')[-1] # [-1] selects the last element of the list print(firstname) print(lastname) 'Chris' + 2 'Chris' + str(2) x = {'Christopher Brooks': 'brooksch@umich.edu', 'Bill Gates': 'billg@microsoft.com'} x['Christopher Brooks'] # Retrieve a value by using the indexing operator x['Kevyn Collins-Thompson'] = None x['Kevyn Collins-Thompson'] for name in x: print(x[name]) for email in x.values(): print(email) for name, email in x.items(): print(name) print(email) x = ('Christopher', 'Brooks', 'brooksch@umich.edu') fname, lname, email = x x = ('Christopher', 'Brooks', 'brooksch@umich.edu', 'Ann Arbor') fname, lname, email = x more on strings sales_record = { 'price': 3.24, 'num_items': 4, 'person': 'Chris'} sales_statement = '{} bought {} item(s) at a price of {} each for a total of {}' print(sales_statement.format(sales_record['person'], sales_record['num_items'], sales_record['price'], sales_r