Thursday, June 25, 2015

Python Crash Course - part one

Lecture 2
Lecture 3 (functional programming)



"The quality of our thoughts is bordered on all sides
by our facility with language."

           --J. Michael Straczynski

# How to comment?

# What is a data type?

'''
1. Variables, assignments and expressions

- Neither a variable type declaration nor an end-of-line delimiter is necessary.
- The line is ended by the end of the line.
- Variables are created automatically when they're first assigned.
- A new assignment overrides any previous assignments.
- Assignment do not return value.
'''

x = "Hello"
print x

del x
# print x and read the traceback

# Expressions return values
x = 3
y = 5
z = (x + y) / 2


"""
2. Indentation and block structuring

Python doesn't use braces to indicate code structure; instead,
the indentation itself is used.

- It's impossible to have missing or extra braces.
- Easy to grasp the skeleton of code just by looking at it.
- Python coding styles are mostly uniform – no flavour wars!

"""

# NOTE: With assignment you include time!
# You say: "That is the value at *this* moment". This is dangerous - SICP for more details.
n = 9
r = 1
while n > 0:
    r = r * n
    n = n - 1

"""
3. Strings

Like most other programming languages, indicates strings through
the use of " or '

- You can't split a normal string across lines - use triple-quoted strings.

"""

# Backslashes can be used to escape characters, to give them special meanings.
#
# \n means the newline character
# \t means the tab character
# \\ means a single normal backslash character
# \" is a plain double-quote character

# Use 'print x' do not type only x

x = "\tDon't need a backslash\n"
x = 'Can\'t get by without a backslash'
x = "Backslash your \" character!"
x = 'You can leave the " alone'

"""
4. Numbers

- Python offers four kinds of numbers: integers, floats, complex numbers, and Booleans.
- Integers can be arbitrarily large
- The division of two integers results in a float
- Built-in numeric functions:

abs, divmod, cmp, coerce, float, hex, int, long, max, min, oct, pow, round

"""

# This is impossible integer in Java
x = 30000000000
x = x / 2.3

# Build in numeric functions
round(2.3)

# Booleans: True, False

# Most Python objects can be used as Booleans!

# The numbers 0, 0.0, and 0+0j are all False; any other number is True.
# The empty string "" is False; any other string is True.
# The empty list [] is False; any other list is True.
# The empty dictionary {} is False; any other dictionary is True.
# The empty set set() is False; any other set is True.
# The special Python value None is always False.

"""
5. 'None' value

- Like zero in arithmetic

This is enough for now.
"""

"""
6. Lists are like arrays

- Ordered collection of objects
- Lists can contain different types of elements

NOTE: Lists are recursive structure - list = element | list
"""

# Note that you don't have to worry about declaring the list
# or fixing its size ahead of time. Following example creates the list
# as well as assigns it, and a list automatically grows or shrinks in size as needed.

# empty one
x = []

# This assigns a three-element list to x
x = [1, 2, 3]

# First element is a number, second is a string, third is another list.
x = [2, "two", [1, 2, 3]]

# Python indexing is more flexible than C indexing; if indices are negative
# numbers, they indicate positions counting from the end of the list,
# with –1 being the last position in the list, –2 being the second-to-last position,
# and so forth.

x = ["first", "second", "third", "fourth"]
a = x[-1]
b = x[-2]

help([]) # or help(list)


"""
7. List slicing
"""

# Can extract or assign to an entire sublist at once, an operation known as slicing.
# Instead of entering list[index] to extract the item just after index,
# enter list[index1:index2] to extract all items including index1
# and up to (but not including) index2 into a new list.

x = ["first", "second", "third", "fourth"]
x[1:-1]
x[0:3]
x[-2:-1]

# When slicing a list, it's also possible to leave out index1 or index2.
# Leaving out index1 means "go from the beginning of the list,"
# and leaving out index2 means "go to the end of the list":

x[:3]
x[2:]

# What about x[:]?

"""
8. Modifying lists

- You can use list index notation to modify a list as well as to extract
an element from it.
- You can also use slicing
"""

x = [1, 2, 3, 4]
x[1] = "two"

# slicing
x = [1, 2, 3, 4]
x[len(x):] = [5, 6, 7]

# NOTE: len() is generic function for all type of sequences

# append at the beginning
x[:0]

# append at the end
x = [1, 2, 3]
x.append("four")

# if append list?
y = [5, 6, 7]
x.append(y)

x = [1, 2, 3]
x.extend(y)

# or use '+'
z = [1, 2, 3] + [4, 5]

# insertion
x = [1, 2, 3]
x.insert(2, "hello")
x.insert(0, "start")

# The 'del' statement is the *preferred* method of deleting list items or slices
x = ['a', 2, 'c', 7, 9, 11]
del x[1]
del x[:2]

# As a homework read about 'remove'

"""
9. List membership with the in operator

- To test if a value is in a list using the 'in' operator, which returns a Boolean value.
"""

3 in [1, 3, 4, 5]
3 not in [1, 3, 4, 5]
3 not in ["one", "two", "three"]

"""
10. List initialization with the * operator
"""
z = [None] * 4
z = [3, 1] * 2 # list multiplication operator


"""
11. Tuples

Tuples are data structures that are very similar to lists, but they
can't be modified. They can only be created.

- The main difference between tuples and lists is that tuples are immutable.
"""

x = ('a', 'b', 'c')

# REMEMBER: One-element tuples need a comma!

x = 3
y = 4
(x+y)    # adds x and y
(x + y,) # Including a comma indicates the parentheses denote a tuple

"""
12. Packing and unpacking tuples

"""

(one, two, three, four) = (1, 2, 3, 4)

# even simpler
one, two, three, four = 1, 2, 3, 4


"""
13. Converting between lists and tuples

It is important to stay immutable!
"""

list((1, 2, 3, 4))
tuple([1, 2, 3, 4])

# 'list' is a convenient way to break a string into characters
list("Hello")


"""
14. Sets

A set in Python is an unordered collection of objects used in situations where member-
ship and uniqueness in the set are main things you need to know about that object.

ATTENTION: Just as with dictionary keys (as you'll see later), the items in a set
must be immutable and hashable. This means that ints, floats, strings, and tuples can be
members of a set, but lists, dictionaries, and sets themselves can't
"""

# sets also have several set-specific operations
x = set([1, 2, 3, 1, 3, 5])
y = set([1, 7, 8, 9])

# union
x|y

# intersection
x&y

# not in both - symmetric difference
x^y

# Frozensets - Because frozensets are immutable and hashable, they can be members of other
# sets:
x = set([1, 2, 3, 1, 3, 5])
z = frozenset(x)
x.add(z)


"""
15. Strings

Strings can be considered sequences of characters, which means you can use index or slice
notation.

REMEMBER: Strings are immutable!
"""

x = "Hello"
x[0]
len(x)
8 * "Победа! "

"""
16. String methods
"""

" ".join(["join", "puts", "spaces", "between", "elements"])
x = "You\t\t can have tabs\t\n \t and newlines \n\n mixed in"
x.split()

help(split) # error
help("".split()) # something different

help("".split)

# Legacy code: string.split(text)

# Converting strings to numbers
float('123.456')

# Getting rid of extra whitespace
x = " Hello, World\t\t "
x.strip()
x.lstrip()
x.rstrip()

# String searching
x = "Mississippi"

# 'find' takes one required argument: the substring being searched for. 'find' returns the
# position of the first character of the first instance of substring in the string object,
# or –1 if substring doesn't occur in the string
x.find("ss")

help("".find)

# with optional parameter
x.find("ss", 3)

# count is used identically to any of the previous four functions but returns the number
# of non-overlapping times the given substring occurs in the given string
x.count("ss")

x.startswith("Miss")
x.endswith("pi")

"""
17. Modifying strings?!

Strings are immutable, but string objects have a number of methods that can operate on
that string and return a new string that's a modified version of the original string.
"""

x.replace("ss", "+++")

# Cool - Modifying strings with list manipulations

text = "Hello, World"

# Make it mutable list
wordList = list(text)
wordList[6:] = []
wordList.reverse()
text = "".join(wordList)
print text

"""
18. Using the format method

The format command is a powerful string-formatting mini-language and offers almost endless
possibilities for manipulating string formatting.
"""

# The format method and positional parameters
"{0} is the {1} of {2}".format("Ambrosia", "food", "the gods")

# The format method and named parameters
"{food} is the food of {user}".format(food="Ambrosia", user="the gods")

# ... it is powerful
"{0} is the food of {user[1]}".format("Ambrosia", user=["men", "the gods", "others"])

# The format specifier lets you control the fill character, alignment, sign, width,
# precision, and type of the data when it's substituted for the replacement field.

# will skip format specifiers...

# Formatting strings with %
"%s is the %s of %s" % ("Ambrosia", "food", "the gods")


"""
19. Dictionaries

Dictionaries are also called associative arrays or hashtables in other languages.
"""

# Empty dictionary is created much like an empty list, but with curly braces instead of
# square brackets

x_list = []
y_dict = {}

help(dict)

# Keys
x = {0: 'zero', 1: 'one'}

# Whereas list indices must be integers, dictionary keys are much less restricted -
# they may be numbers, strings, or one of a wide range of other Python objects.

y = {}
y["two"] = 2
y["pi"] = 3.14
y["two"] * y["pi"]

len(y)

english_to_french = {'red': 'rouge', 'blue': 'bleu', 'green': 'vert'}

# keys in a list
list(english_to_french.keys())

# values in a list
list(english_to_french.values())

# both
list(english_to_french.items())

# 'in' checks in keys
'red' in english_to_french

# Attempting to access a key that isn't in a dictionary is an error in Python!

# Alternatively, you can use the 'get' function. It returns the value associated with a key,
# if the dictionary contains that key, but returns its second argument if the dictionary
# doesn't contain the key:

print english_to_french.get('chartreuse', 'No translation')

# The difference between 'get' and 'setdefault' is that after the setdefault call, there
# is a key in the dictionary 'chartreuse' with the value 'No translation'.

print english_to_french.setdefault('chartreuse', 'No translation')

# What can be used as a key?

# Any Python object that is immutable and hashable can be used as a key to a dictionary!

"""
20. Control flow
"""

# WHILE LOOP

# while condition:    <-- : means that control structure continues
#     body
# else:               <-- difference is here (optional)
#     post-code

# 'condition' is an expression that evaluates to a true or false value. As long as it's
# True, the 'body' will be executed repeatedly. If it evaluates to False, the while loop
# will execute the 'post-code' section and then terminate.

# The break and continue statements in body or pass

# The IF-ELIF-ELSE statement

# if condition1:
#     body1
# elif condition2:
#     body2
# elif condition3:
#     body3
# .
# .
# elif condition(n-1):
#     body(n-1)
# else:
#    body(n)

# The FOR loop

# for item in sequence:
#     body
# else:
#     post-code     <-- optional

x = [1, 3, -7, 4, 9, -5, 4]
for i in range(len(x)):          # range function
    if x[i] < 0:
        print "Found a negative number at index: %s" % i

# enumerate function to loop over both the items and their index
x = [1, 3, -7, 4, 9, -5, 4]
for i, n in enumerate(x):        # enumerate
    if n < 0:
        print "Found a negative number at index: %s " % i

# Comparison and Boolean operators: <, <=, >, >=, ==, !=,  <>, or, and

# The big example :)
def contains_even_number(l):
  "Prints whether or not the list l contains an even number."
  for elt in l:
    if elt % 2 == 0:
      print "list contains an even number"
      break
    else:
      print "list does not contain an even number"

No comments:

algorithms (1) cpp (3) cv (1) daily (4) emacs (2) freebsd (4) java (3) javascript (1) JSON (1) linux (2) Lisp (7) misc (8) programming (16) Python (4) SICP (1) source control (4) sql (1) думи (8)