Python Tutorial

Before we get started, lets look at some quick facts about python:

  • Developed by Guido van Rossum in the late eighties
  • Python is an easy to use high level language
  • We like to use it for AI because of its extensive list of libraries; Tensorflow, Numpy, Pandas, etc.

Lets get Started!

0. Introduction

  • Python is different from many languages since it does not use a semicolon
In [1]:
number = 0 #this is a valid line in python, it might look different from what you're used to
  • In order to print in python you just use the print method
  • if you want to print different variables you seperate it with commas
  • if you want no space between print statements you have to declare it
In [2]:
print("Hello World") #Printing a string

name_1 = "Fred" #Declaring two strings
name_2 = "Jason"

print(name_1, name_2) #Printing two variable in one print statement
print(name_1, name_2, sep='') #Here we remove the separation between two variables
Hello World
Fred Jason
FredJason

1. Data types

  • Python variable types do not need to be explicitly declared
  • This is different from statically typed languages such as Java because you're not specifying an integer or a string, it is automatically assumed
  • Python has six standard data types

Below we'll see the first 4 data types(Integer, Float, Complex, and boolean):

In [3]:
number = 100 #integer because it's a whole number
other_number = 10.5 #float because it has decimal
complex_num = 9+7j #the j makes it a complex number
bool_variable = False

print(number, "is of type", type(number))
print(other_number, "is of type", type(other_number))
print(complex_num, "is of type", type(complex_num))
print(bool_variable, "is of type", type(bool_variable))
100 is of type <class 'int'>
10.5 is of type <class 'float'>
(9+7j) is of type <class 'complex'>
False is of type <class 'bool'>

Strings

  • Strings are a sequence of characters in python
  • Strings can be represented using either single or double quotes
  • You can use either quote types in a string because it allows you to include quotes inside of a string
  • Using a backslash or escape character() also allows you to use characters you often might not be able to use in a string
In [4]:
sentence = "This is a sentence" #example of a string
another_sentence = 'This is another sentence' 

print('"' ,sentence, '"' , "is of type", type(sentence))
print('"' ,another_sentence, "\"" , "is of type", type(another_sentence))
" This is a sentence " is of type <class 'str'>
" This is another sentence " is of type <class 'str'>

Slicing a string

  • Strings can be sliced to either isolate a single character or a section of a string
  • These slices are immuatable and can't be changed
  • Python is 0 indexed so when slicing the first char starts at 0
In [5]:
example = "This is a sentence"

print("example[2] = " , example[2]) #Grabbed single character
print("example[1:7] = " , example[1:7]) #Prints from interval 1 to 7
print("example[:] = " , example[:]) #prints range from 0 to end
example[2] =  i
example[1:7] =  his is
example[:] =  This is a sentence

List

  • A list is a collection of variables denoted by square brackets
  • This is similar to an ArrayList in Java
  • The data types stored within the lists can all be different
In [6]:
example_list = [1, 5 , 5 , 9] #This is a list of integers
different_data_types = [1 , 20.5, "Hello World", False] #This list has different data types within and it still works

print(example_list, "is of type", type(example_list))
print(different_data_types, "is of type", type(example_list))

#We can also choose a specific index, a range of indexes, or change a value
specific_item = example_list[2] #index 2
list_of_a_list = different_data_types[:2] #range from 0 to 2
example_list[0] = "python is easy" #changing single index

print("\n")
print(specific_item)
print(list_of_a_list)
print(example_list)
[1, 5, 5, 9] is of type <class 'list'>
[1, 20.5, 'Hello World', False] is of type <class 'list'>


5
[1, 20.5]
['python is easy', 5, 5, 9]

List methods

Now lets look at different list methods

  • append: Adds to the end of the list
  • insert: Adds to specified position
  • remove: removes first instance of specified item
  • pop: removes specified index
  • copy: copies list
  • clear: empties list

These are only a few of the many built-in methods that lists provice

In [7]:
this_list = [1 , 2 , 3 , 4 , 5 , 1]
print("original list: ", this_list)

this_list.append("random string") #adds string to end of list
print("append method: ", this_list)

this_list.insert(3, False) #insert false at index 3
print("insert method:", this_list)

this_list.remove(1) #remove the number 1
print("remove method: ", this_list)

this_list.pop(2) #if nothing is specified then last item is popped off
print("pop method: ", this_list)

copy_this_list = this_list.copy() #stores copy of list in variable
print("copy method: ", copy_this_list)

this_list.clear()
print("clear method: ", this_list)
original list:  [1, 2, 3, 4, 5, 1]
append method:  [1, 2, 3, 4, 5, 1, 'random string']
insert method: [1, 2, 3, False, 4, 5, 1, 'random string']
remove method:  [2, 3, False, 4, 5, 1, 'random string']
pop method:  [2, 3, 4, 5, 1, 'random string']
copy method:  [2, 3, 4, 5, 1, 'random string']
clear method:  []

Dictionaries

  • A dictionary is a collection with key value pairs (Key:Value)
  • This is the same as a hashmap in java
  • The hashmap is declared through the use of curly braces
  • Dictionaries are optimized for retrieving data through the use of a key, which returns a value
In [8]:
this_dictionary = {"Key" : "Value" , "REDID" : 1923922 , "Address" : "1521 Drive"} #Declaring dictionary
print(this_dictionary, "is of type", type(this_dictionary))

#May also be written like this:
abc_num_conversion = {
    "A":1,
    "B":2,
    "C":3,
    "D":4
}
print(abc_num_conversion, "is of type", type(abc_num_conversion))
{'Key': 'Value', 'REDID': 1923922, 'Address': '1521 Drive'} is of type <class 'dict'>
{'A': 1, 'B': 2, 'C': 3, 'D': 4} is of type <class 'dict'>

Accessing Dictionaries

  • Just like other data structures, there are different methods in order to access and modify dictionaries
  • Below we'll cover the different ways to access and modify a dictionary
  • There are more methods than the ones listed below but these are the most important ones
In [9]:
#Declaring a dictionary
student_id = {1000231:"Jarvis Coleman" , 1023234:"John Doe", 1052342:"Hector Nevarez"}
print("original Dictionary: ", student_id)

#Adding to a dictionary
student_id[8239023] = "Alex Navarro" #Adds key value pair to end
print("Adding Item: ", student_id)

#Removing Item
student_id.pop(1052342) #Providing key removes the Key:Value pair
print("Removing Item: ", student_id)

student_id.popitem() #Pops last Key:Value pair in dictionary
print("Popping Item: ", student_id)

#Getting Value from key
name = student_id.get(1023234)
print("Getting Value from key: ", name)
original Dictionary:  {1000231: 'Jarvis Coleman', 1023234: 'John Doe', 1052342: 'Hector Nevarez'}
Adding Item:  {1000231: 'Jarvis Coleman', 1023234: 'John Doe', 1052342: 'Hector Nevarez', 8239023: 'Alex Navarro'}
Removing Item:  {1000231: 'Jarvis Coleman', 1023234: 'John Doe', 8239023: 'Alex Navarro'}
Popping Item:  {1000231: 'Jarvis Coleman', 1023234: 'John Doe'}
Getting Value from key:  John Doe

Other data types

  • There are other data types such as tuple and set but we won't be going over those
  • Sets: If you wan't more information on sets Click Here
  • Tuple: If you want more information on tuples Click Here

2. If Statements

Before we get into If statements lets look at the logical conditions supported in python

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • remainder operator(modulus): 10 % 2 == 0

These logical operators are used to evaluate if statements
For an if statement to execute the if statement must be true

In [10]:
number_one = 100
number_two = 150

if number_one < number_two:
    print("SDSU AI Club")

if number_one % 2 == 0:
    print(100/2)

if 3 % 2 == 1:
    print(3/2)
SDSU AI Club
50.0
1.5

Notice how the if statement is defined by indentation
Python relies on indentation to define the scope of the code instead of curly braces

Elif and else

  • elif stands for else if
  • the "elif" keyword executes if the previous if statement isn't true
  • if the "if" statement is true elif won't execute
  • the "else" keyword executes if no other if or elif statemement is executed
In [11]:
#Here we'll try to get the elif statement to execute
a = 200
b = 300

if a > b: #this won't execute because it is not true
    print("a is larger than b")
    
elif a != b: #this will execute because the if statement above isn't true but this is
    print("a is not equal to b")
a is not equal to b
In [12]:
#Here we'll try to get the else statement to execute

a = 200
b = 300

if a > 1000: #this won't execute
    print("a is greater than 1000")

elif b > 1000: #this won't execute either because it is not true
    print("b is greater than 1000")
    
else: #this will execute because the if and the elif didn't execute
    print("neither a or b are greater than 1000")
neither a or b are greater than 1000

combining conditional statements

  • you can use the "and" keyword requiring both conditional statements to be true
  • you can use the "or" keyword requiring one conditional statement to be true
In [13]:
num1 = 100.2
num2 = 50
num3 = 19
num4 = 77

if num1 > num2 and num3 < num4: #this will execute because both conditions are met
    print("both conditions must be met for this to print")
    
if num1 == num2 or num3 < num4: #this will execute because only one condition is met
    print("only one condition must be true for this to execute")
both conditions must be met for this to print
only one condition must be true for this to execute