Introduction to Python I#

Python is one the world’s most popular programming languages. It is versatile and easy to learn. Python is widely used in :

  1. Data Science

  2. Machine Learning/ Deep Learning

  3. Scientific Computing

  4. Web development

Python’s creator, Guido van Rossum, name it after the BBC television show ‘Monty Python’s Flying Circus.’ and not the snake! And if you look at Python examples and code online, you will find a lot of references to the television show! Let’s start our Python journey by running a one line program:

print("Hello World!")
Hello World!

print() is a built-in function that prints whatever you put between the parentheses to the screen. In this case, it is printing the words/statement ”Hello World!” to the screen.

Another example of a simple one-line operation:

2+2
4

Jupyter Notebook commands#

Execute a cell:
ctrl+enter
shift+enter <- execute the cell and a create new box below

Basic Data Types#

Integers:#

Whole numbers like 1, 2, 3, 4 etc.

Strings:#

Text, words, statements that are enclosed withing quotation marks "" or ''.

Floating Point:#

Fractional or Decimal numbers like 2.3, 3.33, 4.56, 3.14159

Integers#

2
2

Floating point or decimals/fractions#

3.14159 
3.14159

Strings#

"a"
'a'
"Hello World!" #this is a string
'Hello World!'

Note: Comments start with #

#this is a string

Built-in Functions#

What is a Function??
A function is a set of instructions/ code that performs a task/specific action.

Built-in functions are already defined and coded. You can access by “calling” them.

print("Good Morning Tigers!!") #built in function
Good Morning Tigers!!
type(2)
type(3.14159)
type("a")
type("sun and stars")
str

Variables#

Containers for storing values!

Assignment of Variables#

Storing a value in a variable

jar1 = "peppers"
print(jar1)
peppers
type(jar1)
str
jar1 = "sugar"
print(jar1) #printing values stored in variable "jar1"
sugar
jar2 = 2  #storing an integer
print(jar2)
2
type(jar2)
int
"jar3" # a string
'jar3'

Naming variables#

Rules/guidelines:

  • No spaces

  • No Special Characters @ , ! , %

  • Do not name variables after a function name

jar_of_peppers = "peppers"

Arithmetic Operations#

#Built in Math Operations
4.5+3.5 #Addition
4.5-3.5 #Subtraction
4.5*3.5 #Multiplication
4.5/3.5 #Division
1.2857142857142858
a = 4.49
b = 2
a_add = a + b
a_sub = a - b
a_mul = a * b
a_div = a / b
a_add
6.49
#Additional Math Operations
a_quo = a // b #quotient
a_mod = a % b #modulo or remainder
a_pow = a ** b #power or exponent 2**3 = 8
a_quo
2.0

Other advanced Built-in Object/Data Types#

Lists#

test_list = [1,2,3,4]
test_list
[1, 2, 3, 4]
type(test_list)
list
test_list1 = [1, "Hello World", 3.14159, "a"]
test_list1
[1, 'Hello World', 3.14159, 'a']
a_list = [a_add, a_sub, a_mul, a_div]
a_list
[6.49, 2.49, 8.98, 2.245]

Methods#

Subset of built-in functions

c = " wednesday is today "
#c = 2
print(c)
type(c)
 wednesday is today 
str

Checking available Methods for strings: type c. then hit ”Tab” twice

c.
  Cell In[34], line 1
    c.
      ^
SyntaxError: invalid syntax
#c.upper()  #Hit "Tab" twice
c.upper()
' WEDNESDAY IS TODAY '

Checking what a method does c.upper? and execute the code cell.

c.upper() #hit tab twice methods for given variable
' WEDNESDAY IS TODAY '

Information on built-in classes and object types#

type help(str)

#help(str)
#help(list)
a_list
[6.49, 2.49, 8.98, 2.245]
a_list.append(a_quo)

Note: Running the .append cell twice, adds the same value twice to the list

a_list
[6.49, 2.49, 8.98, 2.245, 2.0]
#a_list.remove?
a_list.remove(2.0)
a_list
[6.49, 2.49, 8.98, 2.245]
a_list.append(a_mod)
a_list.append(a_pow)
len(a_list)
6
a_list
[6.49, 2.49, 8.98, 2.245, 0.4900000000000002, 20.160100000000003]

Indexing:

  1. Eggs

  2. Milk

  3. Toothpaste
    NOTE: the index starts at 0

a_list[0] #first value within the list
6.49
a_list[2] #3rd object. 
8.98
a_list[-1]
20.160100000000003

When counting from the right, Index starts at -1

a_list[6]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [54], in <cell line: 1>()
----> 1 a_list[6]

IndexError: list index out of range
a_list[-7]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [55], in <cell line: 1>()
----> 1 a_list[-7]

IndexError: list index out of range
#Slicing , extracting subsets of a list
a_list[0:4]
[6.49, 2.49, 8.98, 2.245]
a_list[3:]
[2.245, 0.4900000000000002, 20.160100000000003]
a_list[:3]
[6.49, 2.49, 8.98]
a_list[1:4]
[2.49, 8.98, 2.245]
a_add*2
12.98
a_list*2
[6.49,
 2.49,
 8.98,
 2.245,
 0.4900000000000002,
 20.160100000000003,
 6.49,
 2.49,
 8.98,
 2.245,
 0.4900000000000002,
 20.160100000000003]

“For” loops#

a_list
[6.49, 2.49, 8.98, 2.245, 2.0, 0.4900000000000002, 20.160100000000003]
new_list=[] ##empty list
print(new_list)
#Indentation 4 spaces
for obj in a_list:
    print(obj)
    new_list.append(2*obj)
    print(new_list)  #contained in for loop, indented
print(a_list) #not contained in for loop, not indented
6.49
[12.98]
2.49
[12.98, 4.98]
8.98
[12.98, 4.98, 17.96]
2.245
[12.98, 4.98, 17.96, 4.49]
2.0
[12.98, 4.98, 17.96, 4.49, 4.0]
0.4900000000000002
[12.98, 4.98, 17.96, 4.49, 4.0, 0.9800000000000004]
20.160100000000003
[12.98, 4.98, 17.96, 4.49, 4.0, 0.9800000000000004, 40.32020000000001]
[6.49, 2.49, 8.98, 2.245, 2.0, 0.4900000000000002, 20.160100000000003]
new_list
[12.98, 4.98, 17.96, 4.49, 4.0, 0.9800000000000004, 40.32020000000001]
obj
20.160100000000003

Conditional statements#

#comparison
x = 3 #Assignment operation
if x == 5:
    print("Value is equal to 5")
#else if
elif x < 5:
        print("Value is less than 5")
else:
    print("Value is greater than 5")
Value is less than 5
#Combine for loops and if else condition statements
num_list = [2,3,47,9,4,7,8,10,1,6,5]
y = 5

for num in num_list:
    if num == y:
        #f-strings
        print(f"{num} is equal to {y}")
    elif num < y:
        print(f"{num} is less than {y}")
    else:
        print(f"{num} is greater than {y}")
2 is less than 5
3 is less than 5
47 is greater than 5
9 is greater than 5
4 is less than 5
7 is greater than 5
8 is greater than 5
10 is greater than 5
1 is less than 5
6 is greater than 5
5 is equal to 5

List Comprehensions

#typical for loop
for num in a_list:
    print(num*2)
12.98
4.98
17.96
4.49
4.0
0.9800000000000004
40.32020000000001
#list comprehension
[print(num*2) for num in a_list]
12.98
4.98
17.96
4.49
4.0
0.9800000000000004
40.32020000000001
[None, None, None, None, None, None, None]
new_list1 = [num*3 for num in a_list]
new_list1
[19.47,
 7.470000000000001,
 26.94,
 6.735,
 6.0,
 1.4700000000000006,
 60.480300000000014]
new_list1 = [num*1.5 for num in new_list1]
new_list1
[29.205,
 11.205000000000002,
 40.410000000000004,
 10.102500000000001,
 9.0,
 2.205000000000001,
 90.72045000000003]