🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

List

A list is a collection which is ordered and changeable that allows duplicate members. Lists are created using square brackets.

List

Sample Codeeven_numbers = [2,4,6,8,10]
print(even_numbers)
print(len(even_numbers)) # Number Of Items In List
duplicate_list = [2,4,6,8,10,2,4,6,8,10]
print(duplicate_list)
print(duplicate_list[1]) # Access List Item By Index Number
print(duplicate_list[2:5]) # Range Of Indexes
even_numbers[5] = 12 # Change Item Value By Index Number
print(even_numbers)
even_numbers.append(14) # Add Item To End
print(even_numbers)
even_numbers.insert(1, 2) # Insert Item Value At Index Number
print(even_numbers)
even_numbers.remove(2) # Remove Item Value
print(even_numbers)
even_numbers.pop(1) # Pop Item At Index Number
print(even_numbers)
del even_numbers.pop[1] # Remove Item At Index Number
print(even_numbers)
even_numbers.clear() # Remove All Items
print(even_numbers)
even_numbers.sort() # Sort All Items In Ascending Order
print(even_numbers)
even_numbers.reverse() # Sort All Items In Reverse Order
print(even_numbers)
copied_list = even_numbers.copy() # Make A Copy
print(copied_list)
list1 = [“John”, “Jake”]
list2 = even_numbers.copy()
list3 = list1 + list2 # Join Two Lists
list1.extend(list2) # Add Elements From One List
print(list1)
print(list3)
print(list1.count(“John”)) # Returns Number Of Times The Value “John” Appears
print(list1.index(“John”)) # Returns Index Of First Time The Value “John” Appears

Table : Common Functions For Manipulating Lists

FunctionDescriptionExample
len()Determine how many itemsprint(len(even_numbers)
append()Append itemseven_numbers.append(12)
insert()Insert item at specified indexeven_numbers.insert(1, 2)
remove()Remove specified itemeven_numbers.remove(2)
pop()Pop the item at specified indexeven_numbers.pop(1)
delDelete the item at specified indexdel even_numbers.pop[1]
clear()Remove all values
even_numbers.clear()
sort()Sort in ascending ordereven_numbers.sort()
reverse()Sort in reverse ordereven_numbers.reverse()
copy()Make a copyeven_numbers.sort()
extend()Add elements from one listlist1.extend(list2)
count()Returns number of elements with specified valuelist1.count(2)
index()Returns index of first element with specified valuelist1.count(4)
# Type Some Code

Chapter 4: Arrays

An array specifies a variable that can be indexed as a list in rows and columns. The first index is zero as is common in most programming languages.

Python does not have an array data type. Instead the data type list, tuple, set or dictionary can be used.

Logical Operators

Python logical operators are used to combine conditional statements. Each operand is a conditional that can be evaluated to a true or false value. The value of the conditions determines the overall value of the operator or grouping.

Logical operators are use to control flow of the application. They are normally found as part of a control statement such as if and while.

Table : Logical Operators

LogicNameExample
andAndnum1 and num2
orOrnum1 or num2
notNotnot(num1 or num2)

Logical Operators

Sample Codenum1 = 5
num2 = 10
if ( num1 > 0 and num2 > 0 ):
	print(“Both AND conditions are true”)
else:
	print(“This condition grouping is false”)
if ( num1 > 0 or num2 > 0 ):
	print(“At least one OR condition is true”)
else:
	print(“Both conditions are false”)
# Type Some Code

Increment And Decrement Operators

Python increment operators are used to increment a variable’s value and decrement operators are used to decrement a variable’s value. Python does not support pre- and post-increment and decrement operators allowing for the increment or decrement of the value by one.

Table : Increment And Decrement Operators

Increment / DecrementNameExample
+=Incrementnum+=1
-=Decrementnum-=1

Increment And Decrement Operators

Sample Codenum = 1
num += 1 # Increment By One
print(num1) # Displays 2
num -= 1 # Decrement By One
print(num1) # Displays 1
# Type Some Code

Comparison Operators

Comparison operators compare two values which can be numbers or strings.

Table : Comparison Operators

ComparisionNameExample
==Equalnum1 == num2
!=Not Equalnum1 != num2
>Greater Thannum1 > num2
<Less Thannum1 < num2
>=Greater Than Or Equal Tonum1 >= num2
<=Less Than Or Equal Tonum1 <= num2

Comparison Operators

Sample Codename1 = “John”
name2 = James”
print(name1 == name2) # Displays False
print(name1 == 10) # Displays False
num1 = 12
num2 = 22
print(num1 > num2) # Displays False
print(num1 == 10) # Displays False
# Type Some Code

Assignment Operators

variables have the same naming convention where the variable is defined. Therefore arithmetic types are scalar types. Arrays (list, tuple and range) are aggregate types.

Table : Assignment Operators

AssignmentEquivalentDescription
num1 = num2num1 = num2Set left operand to same value on right
num1 += num2num1 = num1 + num2Addition
num1 -= num2num1 = num1 – num2Subtraction
num1 *= num2num1 = num1 * num2Multiplication
num1 /= num2num1 = num1 / num2Division
num1 %= num2num1 = num1 % num2Modulus
num1 //= num2num1 = num1 // num2Floor Division
num1 **= num2num1 = num1 ** num2Exponentiation
num1 &= num2num1 = num1 & num2Bitwise AND
num1 |= num2num1 = num1 | num2Bitwise OR
num1 ^= num2num1 = num1 ^ num2Bitwise XOR
num1 >>= num2num1 = num1 >> num2Bitwise Right Shift, Assign Left
num1 <<= num2num1 = num1 << num2Bitwise Left Shift, Assign Right
num1 := num2(num1 = num2)Assign value within expression

Assignment Operators

Sample Codenum1 = 5
num2 = 3
num_equ = num1
num_add += num2
num_equ = num1
num_sub -= num2
num_equ = num1
num_mul *= num2
num_equ = num1
num_div /= num2
num_equ = num1
num_mod %= num2
num_equ = num1
num_flo //= num2
print(num_add) # Displays 8
print(num_sub) # Displays 2
print(num_mul) # Displays 15
print(num_div) # Displays 1.66
print(num_mod) # Displays 2
print(num_exp) # Displays 125
print(num_flo) # Displays 1
# Type Some Code

Arithmetic Operators

The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, floor division and modulus operations.

Table : Arithmetic Operators

OperatorNameExample
+Addition Or Sumnum1 + num2
Subtraction Or Differencenum1 – num2
*Multiplication Or Productnum1 * num2
/Division Or Quotientnum1 / num2
%Modulus Or Division Remaindernum1 % num2
**Exponentiation Or Raising Powernum1 ** num2
//Floor Divisionnum1 // num2

Arithmetic Operators

Sample Codenum1 = 5
num2 = 3
num_add = num1 + num2
num_sub = num1 – num2
num_mul = num1 * num2
num_div = num1 / num2
num_mod = num1 % num2
num_exp = num1 ** num2
num_flo = num1 // num2
print(num_add) # Displays 8
print(num_sub) # Displays 2
print(num_mul) # Displays 15
print(num_div) # Displays 1.66
print(num_mod) # Displays 2
print(num_exp) # Displays 125
print(num_flo) # Displays 1
# Type Some Code

Chapter 3: Operators

An operator is defined to behave like a function but is restricted to specific operations on variables and values.

Some of the operators in Python are arithmetic, assignment, comparison, increment, decrement and logical.

Strings

Python strings are surrounded by either single or double quotation marks.

String literal can be displayed with the built-in print function.

Stings

Sample Codestr1 = “Hello World!”
str2 = ‘Hello World!’
print (str1, str2)

Quotes can be used inside a string only if they do not match the quotes surrounding the string.

Quotes Inside Strings

Sample Codestr1 = “It’s easy”
str2 = “Call me ‘Johnny'”
str3 = ‘Call me “Johnny”‘
print(str1, str2, str3)

Assign a multiline string to a variable using 3 quotes.

Multiline Strings

Sample Codestr1 = “””This is a double quoted
multiple line string
spanning 3 rows.”””
str1 = ”’This is a single quoted
multiple line string
spanning 3 rows.”’
print(str1, str2)

Return a part of the string using the slice syntax. To slice a string, specify the start index and end index separated by a colon. The first character has index 0.

Slicing

Sample Codestr1 = “Hello, World!”
print (str1[2:5]) # Prints from position 2 to position 5
print(str1[:5]) # Prints  from position 0 to position 5
print(str1[2:]) # Prints from position 2 to the end
print(str1[-5:-2]) # Prints from position -5 to position -2

Table : Common Functions For Manipulating Strings

FunctionDescriptionExample
upper()Returns upper casestr2 = str1.upper()
lower()Returns lower casestr2 = str1.lower()
strip()Removes beginning or ending whitespacestr2 = str1.strip()
replace()Replace a string with anotherstr2 = str1.replace(“Hello”, “Join”)
split()Split string into substrings by specified separatorstr2 = str1.split(“,”)

Strings can be combined using string concatenation via the plus “+” symbol

Concatenate Strings

Sample Codestr1 = “Hello”
str2 = “World”
str3 = str1 + str2
str4 = str1 + ” ” + str2
print(str3) # Displays HelloWorld
print(str4) # Displays Hello World

Strings and numbers can be combined using F-strings.

F-Strings Format Strings

Sample Codeage = 24
name = “John”
combined = f”My name is {name}, and I am {age} years old”
print(combined) 

Strings can be escaped using the backslash character “\”.

Table : Escape Characters

CodeDescriptionExample
\’Single Quotestr1 = ‘It\’s okay.’
\”Double Quotesstr1 = “I am \”John\”.”
\\Backslashstr1 = “Insert one \\ backslash.”
\nNew Linestr1 = “Hello\nWorld!”
\rCarriage Returnstr1 = “Hello\rWorld!”
\tTabstr1 = “Hello\tWorld!”
\bBackspacestr1 = “Hello \bWorld!”
\fForm Feedstr1 = “Hello\fWorld!”
\000Octal valuestr1 = “\110\145\154\154\157\40\127\157\162\154\144\41”
\xhhHex valyestr1 = “\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21”

Escape Characters

Sample Codestr1 = “My name is \”Johnny\” but you can call me \”John\”.”
print(str1)
# Type Some Code

Boolean

Booleans represent only 2 values, True or False. Any expression can be evaluated to get one of the two answers, True of False. Empty and zero values will be evaluated as False.

Boolean Values

Sample Codename = “John”
num1 = 5
num2 = 3
print(bool(name)) # Prints True
print(bool(num1)) # Prints True
print(num1 > num2) # Prints True
print(num1 == num2) # Prints False
print(num1 < num2) # Prints False
print(False) # Prints False
print(bool(None)) # Prints False
print(bool(0)) # Prints False
print(bool(“”)) # Prints False
print(bool(())) # Prints False
print(bool([])) # Prints False
print({}) # Prints False
# Type Some Code