In [1]:
type(True)
Out[1]:
In [2]:
type(False)
Out[2]:
In [3]:
True == True
Out[3]:
In [4]:
True == False
Out[4]:
In [5]:
False == False
Out[5]:
In [6]:
5 == 5
Out[6]:
In [7]:
5 == 7
Out[7]:
In [8]:
5 != 7
Out[8]:
In [9]:
5 > 3
Out[9]:
In [10]:
-1 < 0
Out[10]:
In [11]:
1 > 0
Out[11]:
In [12]:
3.14 > 2.72
Out[12]:
In [13]:
3.14 <= 2.72
Out[13]:
In [14]:
'Hello!' != 'Hello!'
Out[14]:
In [15]:
'Hi!' != 'Hello!'
Out[15]:
In [16]:
[1, 2, 3] == [1, 2, 3]
Out[16]:
In [17]:
[1, 2, 3] == [4, 5]
Out[17]:
In [18]:
x = 5 == 7
y = 3 != 0
print(x, y)
In [19]:
z = x or y # Either x or y is True? Yes!
print(z)
In [20]:
z = x and y # Both x and y are True? No!
print(z)
In [21]:
myvar = 5 # Points myvar to object 5
myvar == 3 # Check if myvar is equal to 3
Out[21]:
In [22]:
x = True # Here x points to a bool
y = "True" # Here y points to a str
# z = true # This is an error!!! Here z is undefined.
In [23]:
bool(True) # Duh!
Out[23]:
In [24]:
bool(False) # Duh!
Out[24]:
In [25]:
bool(0)
Out[25]:
In [26]:
bool(5)
Out[26]:
In [27]:
bool(-15)
Out[27]:
In [28]:
bool(0.0)
Out[28]:
In [29]:
bool(3.14)
Out[29]:
In [30]:
bool('') # Is there anything in this string? No!
Out[30]:
In [31]:
bool('Hello') # Is there anything in this string? Yes!
Out[31]:
In [32]:
bool([]) # Is there anything in this list? No!
Out[32]:
In [33]:
bool([1, 2, 3]) # Is there anything in this list? Yes!
Out[33]:
In [34]:
bool(print) # Even a function can be converted to a boolean.
# But why would you do that?
# Well, Python gives you the power!
# You just have to learn how to use it! ;-)
Out[34]:
In [35]:
# help(bool)
In [36]:
type(42)
Out[36]:
In [37]:
b = 42
print(b)
In [38]:
print(dir(int))
Those '__xxx__'
methods are called Python's magic methods.
They are magic in the sense that Python will call them implicitly when you use the corresponding operator.
Some of those methods are a bit advanced, though, and we'll leave it as an exercise to you to learn how to use them.
Let's begin with the basic arithmetic operations.
In [39]:
print('+\t', 29 + 3)
print('-\t', 29 - 3)
print('*\t', 29 * 3)
print('/\t', 29 / 3)
print('%\t', 29 % 3)
print('//\t', 29 // 3)
print('**\t', 29**3)
print('abs()\t', abs(-1))
In [40]:
print('==\t', 1 == 2)
print('!=\t', 1 != 2)
print('<\t', 1 < 2)
print('>\t', 1 > 2)
print('<=\t', 1 <= 2)
print('>=\t', 1 >= 2)
In [41]:
print(int(42)) # __int__ int to int (Duh!)
print(bool(42)) # __bool__ int to bool
print(int(True)) # __int__ bool to int
print(int(3.14)) # __int__ float to int
print(float(42)) # __float__ int to float
print(str(42)) # __str__ int to str
# print(int("Hi!")) # ERROR!!! str to int is not supported!
print(42) # __str__ is implicitly called by print()
In [42]:
i = 42
print(i.real, i.imag)
print(i.numerator, i.denominator)
In [43]:
i = 2**63 - 1 # On 64-bit systems, this is the maximum integer number allowed on most programming languages.
print(i) # This limitation is imposed by the hardware.
In [44]:
i.bit_length() # Number of bits necessary to represent it in binary.
# The extra bit is for the sign, so 1+63 == 64-bits.
Out[44]:
In [45]:
i = 111**222 # In Python integer numbers are implemented in software.
print(i) # So, they have virtually infinite precision.
In [46]:
i.bit_length() # That's why it's possible to have very very very large ints. The only
# limitation is given by how much RAM memory you have installed on your machine.
Out[46]:
In [47]:
# help(int)
float
typeReal numbers are implemented by the float
type. Like all programming languages, a float
is just an approximation to the true value of a real number. This limitation is imposed by the hardware. In Python there is no single precision float
! Here all float
s are 64-bit accurate, i.e., double precision.
In [48]:
type(3.14)
Out[48]:
In [49]:
b = 3.14
print(b)
In [50]:
print(dir(float))
In [51]:
print('+\t', 10.71 + 3)
print('-\t', 10.71 - 3)
print('*\t', 10.71 * 3)
print('/\t', 10.71 / 3)
print('%\t', 10.71 % 3)
print('//\t', 10.71 // 3)
print('**\t', 10.71 ** 3)
print('abs()\t', abs(-1.0))
In [52]:
print('==\t', 3.14 == 5)
print('!=\t', 3.14 != 5)
print('<\t', 3.14 < 5)
print('>\t', 3.14 > 5)
print('<=\t', 3.14 <= 5)
print('>=\t', 3.14 >= 5)
In [53]:
print(float(3.14)) # __float__ float to float (Duh!)
print(bool(3.14)) # __bool__ float to bool
print(float(True)) # __float__ bool to float
print(int(3.14)) # __int__ float to int
print(float(42)) # __float__ int to float
print(str(3.14)) # __str__ float to str
# print(float("Hi!")) # ERROR!!! str to float is not supported!
print(3.14) # __str__ is implicitly called by print()
In [54]:
x, y = 3.14, 5.0
print(x.real, x.imag)
print(x.is_integer()) # Is 3.14 == int(3.14)? No!
print(y.is_integer()) # Is 5.0 == int(5.0)? Yes!
print(x.as_integer_ratio())
print(y.as_integer_ratio())
In [55]:
# help(float)
TODO
A string is a sequence of characters. You can define a string in four different ways:
'Hello'
"Hello"
'''Hello'''
"""Hello"""
Any of those forms is valid in Python, but try not mix them.
Of course, all rules have an exception, p.ex.:
bad:
'you don't know'
better:
'you don\'t know'
best:
"you don't know"
In which case is okay to mix them.
Anyway, whatever format you choose to use just be consistent in you code!
In [56]:
type('Hello')
Out[56]:
In [57]:
s = 'Hello'
print(s)
In [58]:
print(dir(str))
In [59]:
print('==\t', 'Hi' == 'Hello')
print('!=\t', 'Hi' != 'Hello')
print('<\t', 'Hi' < 'Hello') # Humm... what's happening here?
print('>\t', 'Hi' > 'Hello') # Humm... what's happening here?
print('<=\t', 'Hi' <= 'Hello') # Humm... what's happening here?
print('>=\t', 'Hi' >= 'Hello') # Humm... what's happening here?
In [60]:
# String comparison uses lexicographical ordering: first the first two items
# are compared, and if they differ this determines the outcome of the comparison.
# If they are equal, the next two items are compared, and so on, until either
# sequence is exhausted.
print(ord('H'), ord('i'))
print(ord('H'), ord('e'), ord('l'), ord('l'), ord('o'))
In [61]:
help(ord)
In [62]:
ord('H') < ord('H') or ord('i') < ord('e') # A-Ha!!! Now we know what happened there!
Out[62]:
In [63]:
ord('H') > ord('H') or ord('i') > ord('e')
Out[63]:
In [64]:
ord('H') <= ord('H') and ord('i') <= ord('e')
Out[64]:
In [65]:
ord('H') >= ord('H') and ord('i') >= ord('e')
Out[65]:
Remember! String comparison uses lexicographical ordering. Lexicographical ordering for strings uses the Unicode code point number to order individual characters. However...
...most of times what you may want to compare is the length of strings!
In [66]:
print(len('Hi'), len('Hello'), len('abracadabra'))
In [67]:
print('==\t', len('Hi') == len('Hello'))
print('!=\t', len('Hi') != len('Hello'))
print('<\t', len('Hi') < len('Hello'))
print('>\t', len('Hi') > len('Hello'))
print('<=\t', len('Hi') <= len('Hello'))
print('>=\t', len('Hi') >= len('Hello'))
In [68]:
help(len)
In [69]:
x = 'Py'
y = 'thon'
z = 'is'
w = 'ok!'
print(x + y + z + w)
In [70]:
print(x + y + ' ' + z + ' ' + w)
In [71]:
x = 'Py'
y = 'thon ' # Note the white space
z = 'is ' # Note the white space
w = 'nice!'
print(x + y + z + w)
In [72]:
x = 'Py'
y = 'thon'
z = 'is'
w = 'awesome!'
print(' '.join([x + y, z, w]))
In [73]:
print(' -*- '.join([x + y, z, w]))
In [74]:
s = 'Python -*- is -*- awesome!'
In [75]:
'-*-' in s
Out[75]:
In [76]:
'Python' in s
Out[76]:
In [77]:
'Hello' in s
Out[77]:
In [78]:
s in s # Duh!
Out[78]:
In [79]:
'Ha ' * 5
Out[79]:
In [80]:
'Ha ' * 5 + 'Ha!!!'
Out[80]:
In [81]:
s = 'aWeSoMe!'
print(s.lower(), s.upper(), s.capitalize())
In [82]:
s = 'Python -*- is -*- awesome -*- and -*- I -*- love -*- it!'
print(s)
In [83]:
print(s.split(' '))
In [84]:
print(s.split('-*-'))
In [85]:
s.split('-*-', 1) # split by at most 1 time.
Out[85]:
In [86]:
s.split('-*-', 3) # split by at most 3 times.
Out[86]:
In [87]:
help(str.split)
In [88]:
print(s)
In [89]:
s.startswith('Py')
Out[89]:
In [90]:
s.startswith('Python')
Out[90]:
In [91]:
s.startswith('awesome')
Out[91]:
In [92]:
s.endswith('!')
Out[92]:
In [93]:
s.endswith('it!')
Out[93]:
In [94]:
s.endswith('and')
Out[94]:
In [95]:
S = 'Pithon'
# -^----
# 012345
# S[1] = 'y' # This is an ERROR! You can not change the content of a string!
In [96]:
# It will return a copy of S with all occurrences
# of substring old replaced by new.
ss = S.replace('i', 'y')
print(S)
print(ss)
In [97]:
S = 'Hi! How are you?'
ss = S.replace('Hi!', 'Hello!')
print(S)
print(ss)
replace
all occurrences
In [98]:
S = 'tattarrattat' + ' # ' + 'abracadabra'
ss = S.replace('a', '-')
print(S)
print(ss)
replace
the first 4 occurrences
In [99]:
S = 'tattarrattat' + ' # ' + 'abracadabra'
ss = S.replace('a', '-', 4)
print(S)
print(ss)
replace
the last 5 occurrences
In [100]:
S = 'tattarrattat' + ' # ' + 'abracadabra'
ss = S
ss = ''.join(reversed(ss))
ss = ss.replace('a', '-', 5)
ss = ''.join(reversed(ss))
print(S)
print(ss)
In [101]:
# help(str)