This quick python refresher for DevOps and Data Science is designed for individuals who have previously studied Python and are now looking for a concise, crash-course-style guide as reference or to get back into programming. I hope you find this Python tutorial helpful and engaging. Feel free to share your feedback in the comments—I’d love to hear if there’s anything you’d like to see added or improved.

Python Variables & Data Types
We will start with the foundation of any language, i.e. Variables and Data Types.
Variables
Variables are the container for storing values. You can store strings, numbers, Boolean… any value in it.
Declaring a variable
<var_name> = <var_value> # format for declaring a variable
name = 'John' # declaring name as variable to store text value 'John'
age = 10 # declaring variable age to store integer value 10
You can not use python’s reserved keywords to name your variable. Click Here to get the full list. For example, naming your variable as “else” or “class” will throw an error.
When declaring a variable all that you need is the name of the variable and it’s value. Wondering where is the ‘type’ of the variable? How does Python know what type of the variable is it?
Python is Dynamically Typed language
Unlike in other languages, like Java, where you must explicitly declare the data type of a variable (e.g., int i = 10
), Python uses dynamic typing. This means you can simply write i = 10
, and Python will automatically interpret i
as an integer based on the assigned value.
Data Types

As you can see from the image above, you can divide Python Data types into three broad categories:
1. Text
2. Number
3. Boolean
Let’s look at each of them in more detail.
Text Data Type – String
Strings can be represented using either a single quote or double quote, as shown below.
first_name = 'John' #Can use single quote to represent string
employee_name = "Henry" #Can use double quote to represent string
message = "This is John's car" # use double quote when your string itself has single quote in it.
As explained above, if your String value itself has single quote, you can surround the whole string value with double quotes and vice-versa. If no quotes are used within String, then feel free to choose whatever convention you want to go with, that suits you.
Number Data Type – int or float
You can represent numbers in Python as either “int” or “float” types.
age = 20 # int data type
weight = 31.24 # float data type
Boolean Data Type – True or False
In python “True” and “False” are reserved keywords and are used to represent Boolean data type. These are often used in conditions, comparisons and control flow statements.
a = 5 > 3 # Result of comparison statement, a will be assigned the value True
is_active = True # variable is_active is assigned a True value and used in if conditional statement below
if is_active:
print("Value assigned is true")
fun fact: is_active = True is not the same as is_active=true.
Compare the case of “T”. True with uppper case “T” is a boolean data type in python.
Receiving Input From User
We use the input() function for asking for input from the user.
For example, input(“What is your name”). This will prompt the user to input their name. You can then store the value the user inputs by assigning it to a variable. Like shown in the code block below.
name = input("What is your name")
print("Welcome " + name)
Note: input() function always returns string. Even if user enters numerical value, like 10, it will still be treated as string data type by python. To use it as number, you need to cast it to int data type.
Casting / Type Conversion
Python supports converting a variable from one data type to another using built-in functions. Let us look at few most commonly used scenarios below:
1. To Convert string to number: use int() method
birth_year = "1980"
age = 2025 - birth_year # throws error, as birth_year is string, can not use String in mathematical operations
age = 2025 - int(birth_year) # WORKS!! - converting string value to number using int() method
2. To convert string to a floating point number: use float() method
weight_now = "21.34"
diff_weight = 25.12 - weight_now # throws error, as weight_now is string, can not use String in mathematical operations
diff_weight = 25.12 - float(weight_now) # WORKS!! - converting string value to floating point number using float() method
3. To convert a value to boolean: use bool() method
# Code snippets below shows the output of converting 0 and 1 to boolean
bool(0) # False
bool(1) # True
#Code snippets below shows output of converting an empty String and non-empty String when converted to boolean
bool("hello") # True
bool("")
4. To convert a value to String: use str() method
age = 20
print("My age is " + age) # Throws error, as "+" sign in print statement expects a string and not a number data type variable
print("My age is " + str(age)) # WORKS!! after casting, you will see the output as "My age is 20"
Comments
Python supports single line as well as multi line comments.
Single line comment is represented using # as seen in the code blocks above
Multi line comments is represented using “”” (Three double quotes) . Let us look at them in code example below
#Single line comment
name = 'John' # name of the employee. => Single line comment can be used on side as well. Anything after '#' is comment
#Multi line comment
""" Hello this is a
multiline comment.
It will end in this line by three double quote """
Operators
Although there are many types of operators present in Python, just like in other languages, but we will just discuss the two most commonly used operators. You should be familiar with these two at minimum.
Arithmetic Operators
These are used with numeric values to perform common mathematical operations.
print (10 / 3) = 3.333 # divide
print (10 * 3) = 30 # multiply
print (10 % 3) = 1 # mod
print (10 ** 3) = 1000 # Square
Operator precedence
Order in which an arithmetic expression will be evaluated. It will divide the numbers first before multiplying them.
Let us look at an example: If i give Python an expression: “10 * 3 / 2” -> What do you think the answer will be?
Keep the following precedence in mind and run this expression in your favorite Python IDE and check the result with what you had in mind.
bracket -> divide -> multiply -> add -> subtract
Logical operators
Another important construct in Python are the Logical operators. These are used to evaluate conditional statements.
and => (x < 5 and x < 10) # return true of both conditions are true
or => (x < 5 or x > 10) # returns true if any one of the condition is met
not => not(x < 5) # reverses the result, if x is less than 5, then evaluates to false instead of true
Indentation – Important Concept
It is very important to grasp the concept of indentation. Indentation is nothing but white spaces. Python uses indentation to define the scope in the code.
As you’ll see in the code examples below, Python uses indentation to define the scope of code blocks, such as in methods or if
statements. All lines within the same block must be indented at the same level. Let’s look at an example of an if
statement in the next section to understand this better.
Conditional Block: if..elif..else
if statement
a = 10
b = 20
if a < b: # if statement gets executed if condition evaluates to true
print("a is less than b") # FOCUS ON INDENTATION - anything under if needs to be indented so as to be part of if block
print(" I will execute too if a is less than b")
print("I am out of if block, as i am not indented at same level") # this statement is not a part of if block
if .. elif .. else statement
a = 10
b = 20
c = 30
if (a < b):
print(" a is less than b ")
elif ( a < c ):
print(" a is less than c ")
else
print(" a is not less than b or c, none of the condition above was true ")
The code block above should be self-explanatory.
First, if condition is evaluated. If it is true, “a is less than b” is printed. If it is false, then elif condition is evaluated.
Now, elif condition is evaluated. If it is true, “a is less than c” is printed. If it is false, then else condition is executed.
You can use “if .. elif” construct or ” if .. else” construct as well, without using ‘elif’. You necessarily don’t need to use all three at same time. Following code shows use of if..else
if ( a < 10 ):
print(" a is less than 10 ")
else
print(" a is greater than 10 ")
While Loop
Simply put, while loop executes till the condition is evaluated to true.
i = 1
while ( i < 5 ):
print(i)
i = i + 1
print(" Out of while loop, i became equal to 5 ")
As you can see from code block above, while loop will execute till i becomes equal to 5. Same rule of indentation applies in while loop as well.
For loop
Unlike any other languages, For loop in Python is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). We will study all these collections later.
Used more like for traversing a collection. We use For loop for executing a set of statements, once for each item in a list, tuple or collection types.
employees = ["John", "Fred", "Marc"]
for x in employees: # Use for loop to traverse over the list of employees
print(x) # indentation concept followed here as well
print(" for loop ended, i am not indented") # out of scope of for loop
Python Collections
The moment you all have been waiting for, i believe 🙂 Let’s talk about the data structures, the Collections framework in Python Language. So, here it goes…
There are four Collection types in Python. We will get to know each of them in detail in upcoming sections.
- List
- Set
- Tuple
- Dictionary
List
Used to store multiple items of the same data type, in a single variable. Just remember, List is Ordered and allows duplicates
Let us dive right in and look at few important points and operations related to Lists
- Expressed as comma separated sequence of objects enclosed in square brackets
- You can create an empty list by specifying square brackets or using default value of list() type function
- Empty lists are treated as False in Boolean
- Elements are accessed using numeric index
1.Create a List: created using square bracket
employees_list = ["John", "Fred", "Marc"] # List of employees, string data type
age_of_employees = [ 10, 20, 30 ] # List of age of employees, integer data type
2. Access First element of List Use square bracket after list name and specify the index within square brackets.
# <list name>[0] - List index starts with 0
employees_list[0] # retrieves the first element of the list. It will return John
3. Access Last element of List Use square bracket after list name and specify the index within square brackets.
# <list name>[-1] - Negative List index starts from back of the list
employees_list[-1] # retrieves the last element of the list. It will return Marc
4. Print all elements of List You can use for loop to traverse all the elements of the list
for employee_name in employee_list:
print(employee_name)
5. Add item to list use append method.
employee_list.append("Henry")
Tuple
Similar to a list, but you can’t change its contents after it’s created. A tuple is an ordered, immutable collection of elements.
Few key points to remember and operations are as follows:
- Can contain mixed data type, unlike list which has elements of similar data type
- Elements are ordered and immutable
- Use Tuple when you don’t want data to change
- Can be used as dictionary keys as well, unlike lists
1.Create a Tuple
my_tuple = (1, "employee_name", 20) # Created using bracket. Can have multiple data type
my_tuple = 1, "employee_name", 20 # Can also be created without brackets
2. Loop through a Tuple
for item in my_tuple
print(item)
3. Access Elements of Tuple accessed using index in square brackets
print(my_tuple[0]) # outputs 1
4. Check if element exists in Tuple use ‘in’ operator followed by tuple name
20 in my_tuple # outputs True
Sets
Just like List, Sets are used to store multiple items of the same data type. Key Point: Set is used for storing unique group of elements
Few key points and operations related to Sets are below:
- ‘set’ elements must be immutable and unique.
- Think of Sets as Dictionary data types, only with keys but no corresponding values.
- Passing Dictionary to Set will make it lose all the values and will keep its keys only.
- Although it is not ordered, however sorted() returns an ordered list of elements.
- Empty sets are treated as False in Boolean expressions.
- Items can not be changed, only added or removed
1. Create a set set elements are separated by comma, within curly braces.
month_set = {"Jan", "Feb", "Mar"} # Notice the CURLY BRACES to define a set with name of months
2. Loop through the set
for my_month in month_set: # You can use a for loop to traverse the set
print(my_month)
3. Adding new Element
month_set.add("Apr") # The code shows how to add month of Apr in the month set
4. Remove Element from Set Items can not be referred to by index, but by actual value
month_set.remove("Apr") # remove the month Apr from the month set created above
5. Convert a List into Set
set(my_list) # converts the list "my_list" into a set
Dictionary
Simply put, it is used to store key-value pairs. Where keys must be unique and immutable (e.g. strings, numbers, tuples) and values can be of any type including another dictionary itself.
Let us look at few key points and operations related to Dictionary:
- It’s elements are non-ordered sequence of key-value pair
- Lookup times are very fast and internally they use memory efficiently
- Useful where keys are not sequential, as Python uses hashing to map the keys into a sparse data structures
- Dictionary elements are comma separated key:value pair, with key and value separated by colon and within curly braces {}
- Empty Dictionaries are treated as False in Boolean expressions
1. Create a Dictionary
my_dict = { #declare a map with key as String and values as string, integer and boolean
"key1": "value1",
"age": 30,
"is_active": True
}
2. Loop through Dictionary
for key, value in my_dict.items():
print(key, value)
3. Access Value in Dictionary : Items are accessed using keys
print(my_dict["key1"]) # prints the output as value1
print(my_dict["age"]) # prints the output as 30
4. Add or Update a key-value pair in Dictionary
Syntax: <Dictionary Name>["key"] = <new value> # key can be new or existing
my_dict["location"] = "New York"
5. Check if key exists
Syntax: "key" in <dictionary name>
"name" in my_dict # outputs True
6. Remove a Key
del my_dict["age"]
Python Documentation
Click Here to access more such Tutorials and Blog posts