Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

Python Basics

This document provides examples of built-in Python data structures and functions including tuples, lists, dictionaries, sets, and comprehensions. It demonstrates common operations like creating, accessing, modifying, and iterating over collection types. Functions are introduced through examples showing definitions, scopes, returning multiple values, and how functions are objects that can be passed around.

Uploaded by

Girraj Dohare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Python Basics

This document provides examples of built-in Python data structures and functions including tuples, lists, dictionaries, sets, and comprehensions. It demonstrates common operations like creating, accessing, modifying, and iterating over collection types. Functions are introduced through examples showing definitions, scopes, returning multiple values, and how functions are objects that can be passed around.

Uploaded by

Girraj Dohare
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Built-in Data Structures, Functions,

Data Structures and Sequences

Tuple

tup = 4, 5, 6
tup

(4, 5, 6)

nested_tup = (4, 5, 6), (7, 8)
nested_tup

((4, 5, 6), (7, 8))

tuple([4, 0, 2])
tup = tuple('string')
tup

('s', 't', 'r', 'i', 'n', 'g')

tup[0]

's'

tup = tuple(['foo', [1, 2], True])

#tup[2] = False

tup[1].append(3)

tup

('foo', [1, 2, 3], True)

(4, None, 'foo') + (6, 0) + ('bar',)

(4, None, 'foo', 6, 0, 'bar')

('foo', 'bar') * 4

('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')


Unpacking tuples
tup = (4, 5, 6)

a, b, c = tup

tup = 4, 5, (6, 7)

a, b, (c, d) = tup

tmp = a
a = b
b = tmp

a, b = 1, 2

print(a)

print(b)

b, a = a, b

print(a)

print(b)

seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

for a, b, c in seq:

    print('a={0}, b={1}, c={2}'.format(a, b, c))

a=1, b=2, c=3

a=4, b=5, c=6

a=7, b=8, c=9

values = 1, 2, 3, 4, 5

a, b, *rest = values

a, b

rest

[3, 4, 5]

a, b, *_ = values

Tuple methods

a = (1, 2, 2, 2, 3, 4, 2)

a.count(2)

List

a_list = [2, 3, 7, None]

tup = ('foo', 'bar', 'baz')

b_list = list(tup)

print(b_list)

b_list[1] = 'peekaboo'

print(b_list)

['foo', 'bar', 'baz']

['foo', 'peekaboo', 'baz']

gen = range(10)

gen

list(gen)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Adding and removing elements

b_list.append('dwarf')

b_list

['foo', 'peekaboo', 'baz', 'dwarf']

b_list.insert(1, 'red')
b_list

['foo', 'red', 'peekaboo', 'baz', 'dwarf']

b_list.pop(2)

b_list

['foo', 'red', 'baz', 'dwarf']

b_list.append('foo')

print(b_list)

b_list.remove('foo')

print(b_list)

['foo', 'red', 'baz', 'dwarf', 'foo']

['red', 'baz', 'dwarf', 'foo']

'dwarf' in b_list

True

'dwarf' not in b_list

False

Concatenating and combining lists

[4, None, 'foo'] + [7, 8, (2, 3)]

[4, None, 'foo', 7, 8, (2, 3)]

x = [4, None, 'foo']

x.extend([7, 8, (2, 3)])

[4, None, 'foo', 7, 8, (2, 3)]

everything = []
for chunk in list_of_lists:
everything.extend(chunk)

everything = []
for chunk in list_of_lists:
everything = everything + chunk

Sorting

a = [7, 2, 5, 1, 3]

a.sort()

[1, 2, 3, 5, 7]

b = ['saw', 'small', 'He', 'foxes', 'six']

b.sort(key=len)

['He', 'saw', 'six', 'small', 'foxes']

Slicing

seq = [7, 2, 3, 7, 5, 6, 0, 1]

seq[1:5]

[2, 3, 7, 5]

seq[3:4] = [6, 3]

seq

[7, 2, 3, 6, 3, 5, 6, 0, 1]

seq[:5]

seq[3:]

[6, 3, 5, 6, 0, 1]

seq[-4:]

seq[-6:-2]

[6, 3, 5, 6]

seq[::2]

[7, 3, 3, 6, 1]

seq[::-1]

[1, 0, 6, 5, 3, 6, 3, 2, 7]

Built-in Sequence Functions

enumerate

i = 0
for value in collection:

do something with value


i += 1

for i, value in enumerate(collection):

do something with value

some_list = ['foo', 'bar', 'baz']

mapping = {}

for i, v in enumerate(some_list):

    mapping[v] = i

mapping

{'bar': 1, 'baz': 2, 'foo': 0}

sorted
print(sorted([7, 1, 2, 6, 0, 3, 2]))

print(sorted('horse race'))

[0, 1, 2, 2, 3, 6, 7]

[' ', 'a', 'c', 'e', 'e', 'h', 'o', 'r', 'r', 's']

zip

seq1 = ['foo', 'bar', 'baz']

seq2 = ['one', 'two', 'three']

zipped = zip(seq1, seq2)

list(zipped)

[('foo', 'one'), ('bar', 'two'), ('baz', 'three')]

seq3 = [False, True]

list(zip(seq1, seq2, seq3))

[('foo', 'one', False), ('bar', 'two', True)]

for i, (a, b) in enumerate(zip(seq1, seq2)):

    print('{0}: {1}, {2}'.format(i, a, b))

0: foo, one

1: bar, two

2: baz, three

pitchers = [('Nolan', 'Ryan'), ('Roger', 'Clemens'),

            ('Curt', 'Schilling')]

first_names, last_names = zip(*pitchers)

print(first_names)

print(last_names)

('Nolan', 'Roger', 'Curt')

('Ryan', 'Clemens', 'Schilling')

reversed

list(reversed(range(10)))

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

dict

empty_dict = {}

d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}

d1

{'a': 'some value', 'b': [1, 2, 3, 4]}

d1[7] = 'an integer'

print(d1)

print(d1['b'])

{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}

[1, 2, 3, 4]

'b' in d1

True

d1[5] = 'some value'

print(d1)

d1['dummy'] = 'another value'

print(d1)

del d1[5]

print(d1)

ret = d1.pop('dummy')

print(ret)

print(d1)

{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 5: 'some value'}

{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 5: 'some value', 'dummy': 'an
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 'dummy': 'another value'}

another value

{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}

print(list(d1.keys()))

print(list(d1.values()))

['a', 'b', 7]

['some value', [1, 2, 3, 4], 'an integer']

d1.update({'b' : 'foo', 'c' : 12})

d1

{7: 'an integer', 'a': 'some value', 'b': 'foo', 'c': 12}

Creating dicts from sequences

mapping = {}
for key, value in zip(key_list, value_list):
mapping[key] = value

mapping = dict(zip(range(5), reversed(range(5))))

mapping

{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
Default values

if key in some_dict:
value = some_dict[key]
else:
value = default_value

value = some_dict.get(key, default_value)

words = ['apple', 'bat', 'bar', 'atom', 'book']

by_letter = {}

for word in words:

    letter = word[0]

    if letter not in by_letter:

        by_letter[letter] = [word]

    else:

        by_letter[letter].append(word)

by_letter

{'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}

for word in words:


letter = word[0]
by_letter.setdefault(letter, []).append(word)

from collections import defaultdict


by_letter = defaultdict(list)
for word in words:
by_letter[word[0]].append(word)

Valid dict key types

#hash('string')  # gives error

hash((1, 2, (2, 3)))

#hash((1, 2, [2, 3])) # fails because lists are mutable

1097636502276347782

d = {}

d[tuple([1, 2, 3])] = 5

{(1, 2, 3): 5}

set

set([2, 2, 2, 1, 3, 3])

{2, 2, 2, 1, 3, 3}

{1, 2, 3}
a = {1, 2, 3, 4, 5}

b = {3, 4, 5, 6, 7, 8}

a.union(b)

a | b

{1, 2, 3, 4, 5, 6, 7, 8}

a.intersection(b)

a & b

{3, 4, 5}

c = a.copy()

c |= b

print(c)

d = a.copy()

d &= b

print(d)

{1, 2, 3, 4, 5, 6, 7, 8}

{3, 4, 5}

my_data = [1, 2, 3, 4]

my_set = {tuple(my_data)}

print(my_set)

print(len(my_set))

{(1, 2, 3, 4)}

a_set = {1, 2, 3, 4, 5}

{1, 2, 3}.issubset(a_set)

a_set.issuperset({1, 2, 3})

True

{1, 2, 3} == {3, 2, 1}

True

List, Set, and Dict Comprehensions

result = []
for val in collection:
if
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']

[x.upper() for x in strings if len(x) > 2]

['BAT', 'CAR', 'DOVE', 'PYTHON']

dict_comp = {

set_comp = {

unique_lengths = {len(x) for x in strings}

unique_lengths

{1, 2, 3, 4, 6}

set(map(len, strings))

{1, 2, 3, 4, 6}

loc_mapping = {val : index for index, val in enumerate(strings)}

loc_mapping

{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}

Nested list comprehensions

all_data = [['John', 'Emily', 'Michael', 'Mary', 'Steven'],

            ['Maria', 'Juan', 'Javier', 'Natalia', 'Pilar']]

names_of_interest = []
for names in all_data:
enough_es = [name for name in names if
name.count('e') >= 2]
names_of_interest.extend(enough_es)

result = [name for names in all_data for name in names

          if name.count('e') >= 2]

result

['Steven']

some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

flattened = [x for tup in some_tuples for x in tup]

flattened

[1, 2, 3, 4, 5, 6, 7, 8, 9]

flattened = []

for tup in some_tuples:


for x in tup:
flattened.append(x)
[[x for x in tup] for tup in some_tuples]

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Functions

def my_function(x, y, z=1.5):


if z > 1:
return z * (x + y)
else:
return z / (x + y)

my_function(5, 6, z=0.7)
my_function(3.14, 7, 3.5)
my_function(10, 20)

Namespaces, Scope, and Local Functions

def func():
a = []
for i in range(5):
a.append(i)

a = []
def func():
for i in range(5):
a.append(i)

a = None

def bind_a_variable():

    global a

    a = []

bind_a_variable()

print(a)

[]

Returning Multiple Values

def f():
a = 5
b = 6
c = 7
return a, b, c

a, b, c = f()

return_value = f()

def f():
a = 5
b = 6
c = 7
return {'a' : a, 'b' : b, 'c' : c}

Functions Are Objects

states = ['   Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',

          'south   carolina##', 'West virginia?']

import re

def clean_strings(strings):

    result = []

    for value in strings:

        value = value.strip()

        value = re.sub('[!#?]', '', value)

        value = value.title()

        result.append(value)

    return result

clean_strings(states)

['Alabama',

'Georgia',

'Georgia',

'Georgia',

'Florida',

'South Carolina',

'West Virginia']

def remove_punctuation(value):

    return re.sub('[!#?]', '', value)

clean_ops = [str.strip, remove_punctuation, str.title]

def clean_strings(strings, ops):

    result = []

    for value in strings:

        for function in ops:

            value = function(value)

        result.append(value)

    return result

clean_strings(states, clean_ops)

['Alabama',

'Georgia',

'Georgia',

'Georgia',

'Florida',

'South Carolina',

'West Virginia']

for x in map(remove_punctuation, states):

    print(x)

Alabama

Georgia

Georgia

georgia

FlOrIda

south carolina

West virginia

Anonymous (Lambda) Functions

def short_function(x):
return x * 2

equiv_anon = lambda x: x * 2

def apply_to_list(some_list, f):


return [f(x) for x in some_list]

ints = [4, 0, 1, 5, 6]
apply_to_list(ints, lambda x: x * 2)

strings = ['foo', 'card', 'bar', 'aaaa', 'abab']

strings.sort(key=lambda x: len(set(x)))

strings

['aaaa', 'foo', 'abab', 'bar', 'card']

Currying: Partial Argument Application

def add_numbers(x, y):


return x + y

add_five = lambda y: add_numbers(5, y)

from functools import partial


add_five = partial(add_numbers, 5)

Generators

some_dict = {'a': 1, 'b': 2, 'c': 3}
for key in some_dict:

    print(key)

dict_iterator = iter(some_dict)

dict_iterator

<dict_keyiterator at 0x7f5575e1b290>
list(dict_iterator)

['a', 'b', 'c']

def squares(n=10):

    print('Generating squares from 1 to {0}'.format(n ** 2))

    for i in range(1, n + 1):

        yield i ** 2

gen = squares()

gen

<generator object squares at 0x7f5575df9a50>

for x in gen:

    print(x, end=' ')

Generating squares from 1 to 100

1 4 9 16 25 36 49 64 81 100

Generator expresssions

gen = (x ** 2 for x in range(100))

gen

<generator object <genexpr> at 0x7f5575e1d850>

def _make_gen():
for x in range(100):
yield x ** 2
gen = _make_gen()

sum(x ** 2 for x in range(100))

dict((i, i **2) for i in range(5))

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

itertools module

import itertools

first_letter = lambda x: x[0]

names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']

for letter, names in itertools.groupby(names, first_letter):

    print(letter, list(names)) # names is a generator

A ['Alan', 'Adam']

W ['Wes', 'Will']

A ['Albert']

S ['Steven']

Errors and Exception Handling

float('1.2345')

#float('something')   # ERROR

1.2345

def attempt_float(x):

    try:

        return float(x)
    except:

        return x

print(attempt_float('1.2345'))

print(attempt_float('something'))

1.2345

something

#float((1, 2))   # ERROR

def attempt_float(x):

    try:

        return float(x)
    except ValueError:

        return x

#attempt_float((1, 2))   #ERROR

def attempt_float(x):

    try:

        return float(x)
    except (TypeError, ValueError):

        return x

f = open(path, 'w')

try:
write_to_file(f)
finally:
f.close()

f = open(path, 'w')

try:
write_to_file(f)
except:
print('Failed')
else:
print('Succeeded')
finally:
f.close()

Exceptions in IPython

You might also like