Python Basics Revised

This notebook covers Python primitive types

Lists are Python data structures similar to arrays in other languages

part of #100DaysofCode Python Edition follow along at https://jcutrer.com/100daysofcode

In [1]:
# type() reveals a variable's data type
a = 1
b = 1.243454554365756
c = 'Hello String'

print(type(a))
print(type(b))
print(type(c))
<class 'int'>
<class 'float'>
<class 'str'>

Integers

In [2]:
a = 1
b = 10

# adding two ints returns an int
print(type(a + b))
<class 'int'>

Casting Integers

In [4]:
a = 1

# Try to add(+) a string and an integer will raise a TypeError
print(a + " dollars")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-bbee26e360e4> in <module>
      2 
      3 # Try to add(+) a string and an integer will raise a TypeError
----> 4 print(a + " dollars")

TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [8]:
a = 1

# Use str() to cast an integer to a string
print( str(a) + " dollar")
1 dollar

Basic Math

In [9]:
# Add +
print(20 + 10)

# Substract
print( 20 - 10)

# Multiply *
print( 20 * 10)

# Division /
print(20 / 10)

# Division always returns a float
print(type(20 / 10))

# Even if the answer is a whole number
print(20 / 3)
print(type(20 / 3))
30
10
200
2.0
<class 'float'>
6.666666666666667
<class 'float'>

Strings

String in python can be surrounded by either single and doube quotes, both work the same.

PEP8 does not make a recommendation regarding quotes, use whichever one feels right to you.

In [10]:
str1 = 'A string'
print(type(str1))
print(str1)

str2 = 'is a string'
print(type(str2))
print(str2)

# + operator concatenates two strings and returns type 'str'
print(type(str1 + str2))
print(str1 + str2)

# sending multiple arguments will output with a space seperator
print( str1, str2)
<class 'str'>
A string
<class 'str'>
is a string
<class 'str'>
A stringis a string
A string is a string

range()

I always get tripped up with range()

In [11]:
# Range returns a sequence of numbers
a=range(5)
# range() does not return a list, it's an instance of the range class
print(type(a))
print('len=', len(a))
print(len(a))
for i in a:
    print(i)
<class 'range'>
len= 5
5
0
1
2
3
4
In [12]:
b=range(0, 5)
print(b)
print(len(b))
for i in b:
    print(i)
    
range(0, 5)
5
0
1
2
3
4
In [13]:
# This doesnt work
c=range(,5)
print(c)
print(len(c))
for i in c:
    print(i)
  File "<ipython-input-13-8c2c385dbfe6>", line 2
    c=range(,5)
            ^
SyntaxError: invalid syntax
In [14]:
# range class several useful attributes
daysofcode = range(1,100)

print(daysofcode.start)
print(daysofcode.stop)
print(daysofcode.step)
1
100
1
In [15]:
# Range in Reverse, almost
b = range(10, 1, -1)

for i in b:
    print(i)
10
9
8
7
6
5
4
3
2
In [16]:
# In reverse it will need to be range(10, 0, -1). but what about 0???
b = range(10, 0, -1)

for i in b:
    print(i)
10
9
8
7
6
5
4
3
2
1
In [17]:
# Negative range will step up towards 0
b = range(-10, 1)

for i in b:
    print(i)
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
In [18]:
# Negative range with negative step also steps up towards 0 but stops at -2
# I'm really confused by this behaviour????
b = range(-10, -1)

for i in b:
    print(i)
-10
-9
-8
-7
-6
-5
-4
-3
-2

String encoding types ascii, utf8, bytes

In [19]:
str1 = 'Hello World'
print(type(str1))
print(str1)

print() # blank line

str2 = b'Hello World'
# A byte string is not of type str
print(type(str2))
print(str2)
<class 'str'>
Hello World

<class 'bytes'>
b'Hello World'
In [20]:
# Comparing regular string and byte strings

print("\nLet's compare str1 to str2...")

if str1 == str2:
    print('They Match')
else:
    print('They do not match')
Let's compare str1 to str2...
They do not match
In [21]:
# Converting byte strings to reglar string
print(type(str2))

print(str2.decode())   # .decode(encoding=utf-8) 
print(type(str2.decode()))

# Encode regular string to bytes
print(str1.encode())
print(type(str1.encode()))
<class 'bytes'>
Hello World
<class 'str'>
b'Hello World'
<class 'bytes'>

This notebook is part of my #100DaysofCode Python Edition project.