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:
- Comments in Python
- Operators in Python
- Variables in Python
- Keywords in Python
- String operations in Python
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 #Addition print(2+3) #Subtraction print(3-2) #Multiplication print(3*2) #Division print(4/2) #Exponent print(2**2)
5 1 6 2.0 4
Comparison operators such as greater than equal to (>=), less than equal to (), 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 print(3>=2) #Less than equal to print(34) #Less than print(2
True False False True True False
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 #AND print(2>3 and 3>4) False #Output #OR print(2>3 or 4>3) True #Output #NOT 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 print(x) 2 #Output x = x + 1 print(x) 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
#Integer x = 2 print(x) 2 #Output print(type(x)) class 'int' #Output #Float x = 0.2 print(x) 0.2 #Output print(type(x)) class 'float' #Output #String x = "How are you?" #using double quotes print(x) How are you? #Output print(type(x)) class 'str' #Output y = 'Hello' # using single quotes print(y) Hello #Output print(type(y)) class 'str' #Output
Keywords in Python
Python has various keywords that convey special meaning and these keywords cannot be used as a variable.
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?" print(a+b) HelloHow are you? #Output
Here, the (+) operator concatenates the two strings together.
print(3*a) 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' print(a+b) 23 #Output print(3*a) 222 #Output print(type(a)) (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' print(type(x)) class 'str' #Output x = int(x) print(x,type(x)) 50 (class 'int') #Output
Here, we can see the number in string format is converted into an integer.
x = 'Hello' print(type(x)) x = int(x) print(x,type(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.