Numbers


In [ ]:
spam = 65              # an integer declaration. Yes, no semicolon ; at the end
print spam             # print is a reserved keyword / statement

In [ ]:
print type(spam)       # this is a functional call

In [ ]:
eggs = 2
print eggs
print type(eggs)

Let's see the numeric operations


In [ ]:
print spam + eggs      # sum

print spam - eggs      # difference

print spam * eggs      # product

print spam / eggs      # quotient

print spam // eggs     # floored quotient

print spam % eggs      # remainder or module

print pow(spam, eggs)  # power (yes, this is how a funtion is called)

print spam ** eggs     # power

In [ ]:
fooo = -2              # negative value
print fooo
print type(fooo)

In [ ]:
print -fooo            # negated

print +fooo            # unchanged

print abs(fooo)        # absolute value

print int(fooo)        # convert to integer

print long(fooo)       # convert to long integer

print float(fooo)      # convert to float

In [ ]:
fooo += 1              # autoincremental (there is no ++)
print fooo

In [ ]:
# More on the quotient

print spam / eggs          # quotient
print spam / float(eggs)   # quotient
print spam // float(eggs)  # floored quotient, aka. integer division

In [ ]:
# More on the operations result type

print type(spam + eggs)
print type(long(spam) + eggs)
print type(float(spam) + eggs)
print type(float(spam) + long(eggs))

Python automatically infers the type of the result depending on operands type


In [ ]:
# Let's try again the power

print eggs ** spam
print type(eggs ** spam)

from sys import maxint
print maxint

In [ ]:
# Let's instantiate other values

spam = 65L             # a long
print spam
print type(spam)

eggs = 2.0             # a float
print eggs
print type(eggs)

spam = 0101             # an integer in octet
print spam
print type(spam)

spam = 0x41            # an integer in hexadecimal
print spam
print type(spam)

In [ ]:
# Let's do more complex operations

print round(spam + eggs / spam, 3)    # round to n digits, 0 by default
print round((spam + eggs) / spam, 3)  # round to n digits, 0 by default
print round(spam + (eggs / spam), 3)  # round to n digits, 0 by default

Use parentheses to alter operations order

STRINGS


In [ ]:
spam = "spam"                    # a string
print spam
print type(spam)

In [ ]:
eggs = '"eggs"'                  # another string
print eggs
print type(eggs)

In [ ]:
eggs = '\'eggs\''                # another string
print eggs

In [ ]:
spam_eggs = "'\tspam\n\teggs'"   # another string
print spam_eggs
print type(spam_eggs)

Remember

String literal are written in ingle or double quotes. It's exactly the same
Backslash \ is the escape character
Escape sequences in strings are interpreted according to rules similar to those used by Standard C


In [ ]:
spam_eggs = r"'\tspam\n\teggs'"  # a raw string
print spam_eggs
print type(spam_eggs)
  • Raw strings are prefixed with 'r' or 'R'
  • Raw strings use different rules for interpreting interpreting backslash escape sequences

In [ ]:
spam_eggs = u"'\tspam\n\teggs'"  # a unicode string
print spam_eggs
print type(spam_eggs)

Unicode strings are prefixed with 'u' or 'U'

  • unicode is a basic data type different than str:
  • str is ALWAYS an encoded text (although you never notice it)
  • unicode use the Unicode character set as defined by the Unicode Consortium and ISO 10646. We could say unicode are encoding-free strings
  • str string literals are encoded in the default system encoding ( sys.getdefaultencoding() ) or using the encoding specified in modules encoding header
  • The module encoding header is set : #-- coding: utf-8 --
  • There is no way to fully check a string encoding (chardet module)
  • Depending on encoding used it is possible to operate with unicode and str together

In [ ]:
spam_eggs = u"'spam\u0020eggs'"  # a unicode string with Unicode-Escape encoding
print spam_eggs
print type(spam_eggs)

WARNING! Note that in Py3k this approach was radically changed:

  • All string literals are unicode by default and its type is 'str'
  • Encoded strings are specified with 'b' or 'B' prefix and its type is 'bytes'
  • Operations mixing str and bytes always raise a TypeError exception

In [ ]:
spam_eggs = """'\tspam
\teggs'"""                       # another string
print spam_eggs
print type(spam_eggs)
 Three single or double quotes also work, and they support multiline text

In [ ]:
spam_eggs = "'\tspam" "\n\teggs'"  # another string
print spam_eggs
print type(spam_eggs)
  • Several consecutive string literals are automatically concatenated, even if declared in different consecutive lines
  • Useful to declare too much long string literals

In [ ]:
spam_eggs = u"'\tspam\n \
\teggs'"  # a unicode string
print spam_eggs
print type(spam_eggs)
Let's see strings operations

In [ ]:
spam = "spam"
eggs = u'"Eggs"'

In [ ]:
print spam.capitalize()                    # Return a copy with first character in upper case

print spam

WARNING! String are immutables. Its methods return always a new copy


In [ ]:
print spam.decode()                        # Decode the str with given or default encoding
print type(spam.decode('utf8'))

In [ ]:
print eggs.encode()                        # Encode the unicode with given or default encoding
print type(eggs.encode('utf8'))

print spam.endswith("am")                  # Check string suffix (optionally use a tuple)

In [ ]:
print eggs.startswith(("eggs", "huevos"))  # Check string prefix (optionally use a tuple)

print spam.find("pa")                      # Get index of substring (or -1)

print eggs.upper()
print eggs.lower()                         # Convert to upper or lower case

print spam.isupper()
print spam.islower()                       # Check if string is in upper or lower case

print repr(" | spam # ".strip())
print repr(" | spam # ".strip(' |#'))      # Remove leading and trailing characters (only whitespace by default)

print spam.isalpha()
print eggs.isalnum()
print spam.isdigit()                       # Check if string is numeric, alpha, both...

In [ ]:
print repr(eggs)
print str(eggs)

 NOTE:

The repr module provides a means for producing object representations with limits on the size of the resulting strings. This is used in the Python debugger and may be useful in other contexts as well.
str Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.


In [ ]:
print "spam, eggs, foo".split(", ")
print "spam, eggs, foo".split(", ", 1)     # Split by given character, returning a list (optionally specify times to split)

print ", ".join(("spam", "eggs", "foo"))   # Use string as separator to concatenate an iterable of strings

Let's format strings


In [ ]:
print "%s %s %d" % (spam, spam, 7)         # This is the old string formatting, similar to C

In [ ]:
print "{0} {0} {1}".format(spam, 7)        # This is the new string formatting method, standard in Py3k

print "{} {}".format(spam, 7.12345)

In [ ]:
print "[{0:16}|{1:16}]".format(-7.12345, 7.12345)  # Use colon and width of formatted value

print "[{0:>16}|{1:<16}]".format(-7.12345, 7.12345)  # Use <, >, =, ^ to specify the alignment of the value

print "[{0:^16.3f}|{1:^16.3f}]".format(-7.12345, 7.12345)  # For floats, use the dot . and the f to specify precission of floats

print "[{0:_^16.3f}|{1:_^16.3f}]".format(-7.12345, 7.12345)  # Specify the filling value before the alignment

print "[{0:^+16.3f}|{1:^+16.3f}]".format(-7.12345, 7.12345)  # Force the sign appearance

print "{0:b} {0:c} {0:o} {0:x}".format(65)  # For integers, specify base representation (binary, unicode character, octal, hexadecimal

TIME TO START WORKING


In [59]:
# use every cell to run and make the assert pass
# Exercise: write the code 
def minimum(a, b):
    """Computes minimum of given 2 numbers.
    
        >>> minimum(2, 3)
        2
        >>> minimum(8, 5)
        5
        """
    # your code here
    
assert minimum(2,3) == 2

In [78]:
def istrcmp(s1, s2):
    """Compare given two strings for equality, ignoring the case.
    
    	>>> istrcmp("python", "Python")
        True
    	>>> istrcmp("latex", "LaTeX")
        True
    	>>> istrcmp("foo", "Bar")
        False
    """
    # your code here

assert istrcmp("HoLa", "hola") == True

In [ ]:
def reverse_text_by_word(text):
    '''given a text, reverse it by words

        >>> reverse_text_by_word('Sparse is better than dense.')
        'dense. than better is Sparse'
    '''
    # your code here

assert reverse_text_by_word('Sparse is better than dense.') == 'dense. than better is Sparse'

In [ ]:
def sum_chars_text(text):
    '''given a text str, add all chars by its code
        tip:
            >>> c = 'c'
            >>> ord(c)
            99
    '''
    # your code here

assert sum_chars_text("Sume de caracteres") == 1728