Python for IT Automation D522 Quick Study Sheet: Key Concepts & Tips
Python for IT Automation Quick Study Sheet
Common Python topics, examples, and study tips for quick review
This guide focuses on common Python topics students often see in Python for IT Automation, with
simple examples and study tips.
1. Variables and Data Types
name = "Joy" # string
age = 25 # integer
price = 19.99 # float
is_ready = True # Boolean
Common types
Item Description
str text
int whole numbers
float decimal numbers
bool True or False
list ordered, changeable collection (mutable)
tuple ordered, unchangeable collection (immutable)
dict key-value pairs
set unordered, unique values
2. Type Checking and Conversion
x = "5"
print(type(x)) # <class 'str'> This checks the type of data
num = int(x) int(x) converts the string into an interger and then add 2 to it
print(num + 2) # 7 giving you the output of 7
Python for IT Automation D522 Quick Study Sheet: Key Concepts & Tips
,Python for IT Automation D522 Quick Study Sheet: Key Concepts & Tips
Common conversions
Item Description
int("10") takes a string and converts it to an integer
float("3.14") takes a string and converts it to a float
str(25) takes an integer and converts it to a string
True # 0 is False and any non-zero numbers is
bool(1)
True
Takes a string (“abc”) and converts it to a list of
list("abc")
individual characters ['a', 'b', 'c']
3. Lists
fruits = ["apple", "banana", "orange"]
print(fruits[0]) # apple fruits[0] gets the first item
print(len(fruits)) # 3 this will output the number of items in the list
fruits.append("grape") append('grape') adds 'grape' to the end of the list
fruits.remove("banana") remove('banana') deletes 'banana' from the list
Useful list methods
fruits = ["apple", "banana"]
fruits.append("orange") # add one item to the end
print(fruits) # ['apple', 'banana', 'orange']
fruits.remove("banana") # remove by value
print(fruits) # ['apple', 'orange']
fruits.insert(1, "grape") # insert at a specific index
print(fruits) # ['apple', 'grape', 'orange']
print(fruits.pop()) # removes and returns last item
print(fruits) # ['apple', 'grape']
numbers = [4, 2, 7, 1]
numbers.sort() # sorts the list in ascending order
print(numbers) # [1, 2, 4, 7]
2
Downloaded by Morris mwangi Ngigi ()
, Python for IT Automation D522 Quick Study Sheet: Key Concepts & Tips
numbers.reverse() # reverses the current order of the list
print(numbers) # [7, 4, 2, 1]
List method reminders
Item Description
.append(x) adds one item to the end of the list
.remove(x) removes the first matching value
.insert(i, x) inserts item at a specific position
.pop() removes and returns an item
.sort() puts items in order and changes the original list
flips the current order and changes the original
.reverse()
list
Create a new list
This example creates a new list with only even numbers
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [] creates an empty list
for num in numbers:
if num % 2 == 0: the % symbol means remainder (if the number divides by 2
even_numbers.append(num) with no remainder it is even)
print(even_numbers) # [2, 4, 6] add this item to the end of the list
Accessing an element from a list
fruits = ["apple", "banana", "orange"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # orange
3
Downloaded by Morris mwangi Ngigi ()
Python for IT Automation Quick Study Sheet
Common Python topics, examples, and study tips for quick review
This guide focuses on common Python topics students often see in Python for IT Automation, with
simple examples and study tips.
1. Variables and Data Types
name = "Joy" # string
age = 25 # integer
price = 19.99 # float
is_ready = True # Boolean
Common types
Item Description
str text
int whole numbers
float decimal numbers
bool True or False
list ordered, changeable collection (mutable)
tuple ordered, unchangeable collection (immutable)
dict key-value pairs
set unordered, unique values
2. Type Checking and Conversion
x = "5"
print(type(x)) # <class 'str'> This checks the type of data
num = int(x) int(x) converts the string into an interger and then add 2 to it
print(num + 2) # 7 giving you the output of 7
Python for IT Automation D522 Quick Study Sheet: Key Concepts & Tips
,Python for IT Automation D522 Quick Study Sheet: Key Concepts & Tips
Common conversions
Item Description
int("10") takes a string and converts it to an integer
float("3.14") takes a string and converts it to a float
str(25) takes an integer and converts it to a string
True # 0 is False and any non-zero numbers is
bool(1)
True
Takes a string (“abc”) and converts it to a list of
list("abc")
individual characters ['a', 'b', 'c']
3. Lists
fruits = ["apple", "banana", "orange"]
print(fruits[0]) # apple fruits[0] gets the first item
print(len(fruits)) # 3 this will output the number of items in the list
fruits.append("grape") append('grape') adds 'grape' to the end of the list
fruits.remove("banana") remove('banana') deletes 'banana' from the list
Useful list methods
fruits = ["apple", "banana"]
fruits.append("orange") # add one item to the end
print(fruits) # ['apple', 'banana', 'orange']
fruits.remove("banana") # remove by value
print(fruits) # ['apple', 'orange']
fruits.insert(1, "grape") # insert at a specific index
print(fruits) # ['apple', 'grape', 'orange']
print(fruits.pop()) # removes and returns last item
print(fruits) # ['apple', 'grape']
numbers = [4, 2, 7, 1]
numbers.sort() # sorts the list in ascending order
print(numbers) # [1, 2, 4, 7]
2
Downloaded by Morris mwangi Ngigi ()
, Python for IT Automation D522 Quick Study Sheet: Key Concepts & Tips
numbers.reverse() # reverses the current order of the list
print(numbers) # [7, 4, 2, 1]
List method reminders
Item Description
.append(x) adds one item to the end of the list
.remove(x) removes the first matching value
.insert(i, x) inserts item at a specific position
.pop() removes and returns an item
.sort() puts items in order and changes the original list
flips the current order and changes the original
.reverse()
list
Create a new list
This example creates a new list with only even numbers
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [] creates an empty list
for num in numbers:
if num % 2 == 0: the % symbol means remainder (if the number divides by 2
even_numbers.append(num) with no remainder it is even)
print(even_numbers) # [2, 4, 6] add this item to the end of the list
Accessing an element from a list
fruits = ["apple", "banana", "orange"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # orange
3
Downloaded by Morris mwangi Ngigi ()