Introduction to Python, comments in python, variables in python, string operators in python, keywords in python, operators in python

Introduction to Python

Python is an open-source, high-level programming language created by Guido Van Rossum and first released in 1991. Python is rapidly becoming the go-to language due to its ease of writing and code readability.  It is a case sensitive language. The below guide to Python is a perfect way to start learning Python where we introduce you to comments, operators, variables, keywords, and string operations in Python.

The article covers the following aspects:

Getting Started with Python

Comments in Python

Comments are the information that we add in our code so that it is easier for the reader to understand. We can comment a single line using the hashtag symbol (#) and comment multiple lines by enclosing the text in a delimiter (“””).

#Single Line Comment
It is a
multi line comment

Operators in Python

There are various operators in Python used in performing different calculations.

Arithmetic operators such as addition (+), subtraction (-), multiplication (*), division (/), remainder (or modulus operator) (%), exponent (**). They follow the BODMAS rule.

Code for Arithmetic Operators in Python

#Arithmetic Operators



Comparison operators such as greater than equal to (>=), less than equal to (<=), greater than (>), less than, equal to (==) and not equal to (!=). We get a Boolean value (True or False) as the output.

Code for Comparison Operators in Python

#Comparison operators
#Greater than equal to
#Less than equal to
#Greater than
#Less than
#Equal to
#Not equal to



Logical Operators such as AND, OR & NOT.  They give us output according to the logical table and the output is a Boolean value.

Code for Logical operators in Python

#Logical Operators
print(2>3 and 3>4)
False       #Output
print(2>3 or 4>3)
True        #Output
print(not 2>3)
True        #Output

Variables in Python

Variable is an identifier of a particular value. We need to assign values to the variables using the assignment operator (=).  We can perform all sorts of calculations using variables as well.  However, Python does not compel us to store a number into a variable for operations but, it is advised to store for the ease of use.

Code for Variables in Python  

#Variables in python
x = 2
2                        #Output
x = x + 1
3                        #Output

Also, here we can see that the value of the variable can be mutated into some other value. This helps to change the value of the variable while performing calculations.

Our variables can be of any type, i.e. we can assign an integer, float, or a string value.  To assign a character or a string value to a variable, we need to use double quotes (“”) or single quotes (‘’).

 We can find out the type of the variable using the type ( ) function.

Code to find the Variable Type in Python

x = 2
2                                 #Output
class 'int'                  #Output
x = 0.2
0.2                            #Output
class 'float'               #Output
x = "How are you?"     #using double quotes
How are you?            #Output
class 'str'                   #Output
y = 'Hello'                 # using single quotes
Hello                         #Output
class 'str'                #Output

Keywords in Python

Python has various keywords that convey special meaning and these keywords cannot be used as a variable.

Introduction to Python, comments in python, variables in python, string operators in python, keywords in python, operators in python
Keywords in Python

String Operations in Python

The operations on a string that we do are not similar to the operations performed on numbers. Both string and numbers operate differently.

Code for String Operations in Python

#String operations
a = "Hello"
b = "How are you?"
HelloHow are you? #Output

Here, the (+) operator concatenates the two strings together.

HelloHelloHello #Output

Here, in this case, the string is printed thrice.

Also, if we write numbers in quotes they act as a string.

a = '2'
b = '3'
23 #Output
222 #Output
(class 'str') #Output

Here, we can see that since the numbers were in quotes they acted as a string. As a result, the type of the variable ‘a’ is shown as a string.

Type Conversion in Python

Suppose, we want to convert the type of some variable, we do that by using type conversion functions.  Some type conversion functions are:

int( ): To convert a floating point or a number written as a string into an integer. We cannot convert a string in an integer format.

Code for converting string to integer in Python

#Type Conversion
x = '50'
class 'str'                  #Output
x = int(x)
50 (class 'int')         #Output

Here, we can see the number in string format is converted into an integer.

x = 'Hello'
x = int(x)
(class 'str')    #Output
ValueError: invalid literal for int() with base 10: 'Hello'     #Output

However, here we see that we get an error while converting a string into an integer. Therefore, we can convert a string into an integer only if it contains a number.

float ( ): To convert a number into float format.

Code to convert a number into float in Python

a = 2
a = float(a)
print(a, type(a))
2.0 (class 'float') #Output
b = '3.2222'
b = float(b)
print(b, type(b))
3.2222 (class 'float') #Output

Here, both the numbers are converted in a float format.

string ( ): To convert the variable into a string.

Code to convert a number into string in Python

c = 2
c = str(c)
print(c, type(c))
2 (class 'str') #Output
d = 2.14
d = str(d)
print(d, type(d))
2.14 (class 'str') #Output

Here, both the variables are converted into a string.

Let us know in the comments if you have any queries. We hope you had a great introduction to python. If you want to start learning R, this article can help you get there.

This article is submitted by Jinal Shah.

Kindly share your views and feedback.

About the Author

Jinal Shah

Graduate in Applied Statistics & Analytics (NMIMS) and Aspiring Actuary

Comments 2

  1. Pingback: Top picks to invest your time to become tomorrow's Actuary • TAC

  2. Pingback: Best Python Courses Online • The Actuarial Club

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.