number = 0 #this is a valid line in python, it might look different from what you're used to
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
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))
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))
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_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)
Now lets look at different list methods
These are only a few of the many built-in methods that lists provice
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)
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))
#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)
Before we get into If statements lets look at the logical conditions supported in python
These logical operators are used to evaluate if statements
For an if statement to execute the if statement must be true
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)
Notice how the if statement is defined by indentation
Python relies on indentation to define the scope of the code instead of curly braces
#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")
#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")
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")