[0] Numbers, strings and booleans

1. Numbers

1.1. Integers


In [ ]:
1

Integers have unlimited precision.


In [ ]:
3784327523490875234592803475234852348795234978572345982347543298057

In [ ]:
print(type(1)); type(1)

1.2. Floats


In [ ]:
1.0

In [ ]:
.01

In [ ]:
1.5E10

In [ ]:
print(type(1.5E10)); type(1.5E10)

Floats have finite precission and are implemented using double in C.

1.3 Complex numbers


In [ ]:
print(1.5 + 0.5j.real, "+", 1.5 + 0.5j.imag,'j')

1.3. Operations

1.3.1. Addition


In [ ]:
1+2

In [ ]:
1.0+2

In [ ]:
1.+2

1.3.2. Multiplication


In [ ]:
2*2

In [ ]:
1.*2

1.3.3. Division


In [ ]:
1/2 # Floating point division

In [ ]:
2/2

In [ ]:
2//2 # Integer division

In [ ]:
3//2

1.3.4. Type casting


In [ ]:
int(1.)

In [ ]:
float(1)

1.3.5. Power


In [ ]:
a = 2**1000
print(a)

1.3.6. Modulo


In [ ]:
3 % 2

1.3.7. Bit shift


In [ ]:
1 << 2 # Left shift

In [ ]:
4 >> 2 # Right shift

1.3.8. Bitwise operations


In [ ]:
a = 0b11100111 &\
    0b10111101 # Bit-wise AND
print(a); print(bin(a))

In [ ]:
a = 0xFA00 | 0x000A # Bit-wise OR
print(hex(a))
print(oct(a))
print(bin(a))
print(type(a))

In [ ]:
print(bin(1)); 0b1 ^ 0b1 # Bit-wise XOR

In [ ]:
~1 # 2's complement invert

In [ ]:
bin(-1)

In [ ]:
bin(-2)

1.3.9. Comparison (booleans)


In [ ]:
1 < 2 # Less than

In [ ]:
1 > 2 # Greater than

In [ ]:
1 <= 1 # Less than or equal to

In [ ]:
1 >= 1 # Greater than or equal to

In [ ]:
1 == 1 # Equal to

In [ ]:
1 != 1 # Not equal to

In [ ]:
# Python has a very good arithmetic accuracy!
print(1/(10*1000) != 1/(10*1000))
print(1/(10*1000) != 1/(10*1000 + 1))

2. Strings

2.1. Definitions


In [ ]:
"hello"

In [ ]:
"""hello"""

In [ ]:
'hello'

In [ ]:
"I'm a string with a single quote inside"

In [ ]:
'I\'m a string with a single quote inside' # \ == Escape character

In [ ]:
'''... I'm a string with both types of "quotation marks" inside!'''

In [ ]:
a = '''I'm "a"'''

print(a)

In [ ]:
a

In [ ]:
len(a)

In [ ]:
a = "hola " \
"caracola"
print(a)

In [ ]:
print(r"I'm a raw string \n.")
print("I'm not \n.")

In [ ]:
print(u"I'm a Unicode string: \u5982\u679c\u6211\u662f")

In [ ]:
print('''I'm a multy line
string''')

In [ ]:
ord('a')

In [ ]:
ord('á')

In [ ]:
ord('果')

In [ ]:
chr(26524)

2.2 Some useful operations


In [ ]:
min('AaBb')

In [ ]:
max('AaBb')

In [ ]:
'hello'.index('e')

In [ ]:
'hello'.index('ll')

In [ ]:
'hello'.find('ll')

In [ ]:
'hello'.count('l')

In [ ]:
list('hello')

In [ ]:
set('hello')

In [ ]:
tuple('hello')

In [ ]:
type('hello')

In [ ]:
type(b'hello')

In [ ]:
'hello'.capitalize()

In [ ]:
'Hello'.lower()

In [ ]:
'Hello'.swapcase()

In [ ]:
'Hello'.upper()

In [ ]:
'the entropy of the Spanish language'.title()

In [ ]:
'hello'.islower()

In [ ]:
'Hello'.islower()

In [ ]:
'Hello'.isupper()

In [ ]:
'HELLO'.isupper()

In [ ]:
'Iron Maiden'.isspace()

In [ ]:
' '.isspace()

In [ ]:
' \n'.isspace()

In [ ]:
'hello'.center(10)

In [ ]:
'hello'.center(10, '.')

In [ ]:
'hello'.endswith('l')

In [ ]:
' hello '.strip()

In [ ]:
' hello'.lstrip()

In [ ]:
' hello '.rstrip()

In [ ]:
'www.hello.com'.lstrip('w')

In [ ]:
'www.hello.com'.lstrip('h')

2.2. Printing


In [ ]:
print('I have a newline character\ninside')

In [ ]:
print('I have a backslash character "\\" inside')

In [ ]:
print ('I', 'love', 'pizza')

In [ ]:
print('hola')
print('caracola')

In [ ]:
print('hola', end=' ')
print('caracola')

In [ ]:
print('hola', end='\n')
print('caracola')

In [ ]:
print('a', 'b', sep='+')

2.3. Comparison


In [ ]:
"hello" == 'hello'

In [ ]:
"""hello""" == 'hello'

In [ ]:
'hello' < 'Hello'

In [ ]:
'f' in 'file'

In [ ]:
'123'.isalnum()

In [ ]:
'a34v'.isalnum()

In [ ]:
'123'.isalpha()

In [ ]:
'a34v'.isalpha()

In [ ]:
'aba'.isalpha()

In [ ]:
'1e10'.isalnum()

In [ ]:
'1e10'.isdigit()

In [ ]:
'124'.isdigit()

In [ ]:
'hello'.startswith('he')

In [ ]:
'hello'.endswith('lo')

2.4. Concatenation


In [ ]:
'hola' + 'caracola'

In [ ]:
'hola' + 1

In [ ]:
'hello' + str(1)

In [ ]:
'hola ' + 'caracola'

2.5. Formating


In [ ]:
'hello {}'.format(1)

In [ ]:
'{} {} {}'.format(1, "dos", 3)

In [ ]:
'{0} {1} {2}'.format(1, "dos", 3)

In [ ]:
'{2} {1} {0}'.format(1, "dos", 3)

In [ ]:
import math
print(math.pi)
print('{:.4f}'.format(math.pi))
print('{:.50f}'.format(math.pi))

In [ ]:
print('{:*^21}'.format('hello'))

In [ ]:
print('{subject} {predicate}'.format(subject='I', predicate='love pizza'))

In [ ]:
a = 4
b = 5
result = a + b
print(f"{a} + {b} = {result}")

2.6. Joining and splitting


In [ ]:
print(":".join(['h', 'e', 'l', 'l', 'o']))

In [ ]:
print('h:e:l:l:o'.split(':'))

In [ ]:
print('hola caracola'.split(' '))

In [ ]:
type('hola caracola'.split(' '))

2.7. Slicing


In [ ]:
print('hello'[::-1]) # [begin:end:step]

In [ ]:
print('hello'[1:2]) # Get a slice!

In [ ]:
print('hello'[:4])

In [ ]:
print('hello'[4:])

Replacing


In [ ]:
'hello'.replace('l', 'y')

In [ ]:
'hello'.replace('l', 'y', 1)

3.8 Strings are inmutable


In [ ]:
s = 'jelly'
id(s)

In [ ]:
s = 'bean'
id(s)

3. Booleans


In [ ]:
True == True # Boolean equal to

In [ ]:
True != True # Boolean not equal to

In [ ]:
not True # Boolean NOT

In [ ]:
True and False # Boolean AND

In [ ]:
True or False # Boolean OR

4. Shortcuts for operations and assigments


In [ ]:
a = 2
a *= 5
print(a)
a += 3
print(a)

In [ ]:
a = 1
a = a + 2

In [ ]:
a = 1
a <<= 1
print(a)

In [ ]:
a = 'hello'
a += 'world'
print(a)

In [ ]:
a = 1
b = 2
a, b = b, a
print(a, b)

List of names in the current local scope: dir()


In [ ]:
dir()

List of valid attributes for a object: dir(<object>)


In [ ]:
dir(a)