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 ease of writing and code readability. It is case sensitive language. The below guide to python is a perfect manner to start learning python where we introduce you to comments, operators, variables, keywords, and string operations in python.
The article where we introduce you to python 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 which can be used to perform operations.
Arithmetic operators such as addition (+), subtraction (-), multiplication (*), division (/), remainder (or modulus operator) (%), exponent (**). It follows the BODMAS rule. Below given is the example for operators in python.
Code for 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>4) #Equal print(2==2) #Not equal to print(2!=2)
True False False False 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 #OR print(2>3 or 4>3) True #NOT print(not 2>3) True
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 ease of use. Below is the example for variables in python.
Code for Variables in python
#Variables in python x = 2 print(x) 2 #Output x = x + 1 print(x) 3 #Output
Also, we can see that the value of the variable can be mutated into some other value.
Our variables can be of any type, i.e. we can assign an integer, float or a string. 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. Below is the example for variable type in python.
Code for variable type in python
#Integer x = 2 print(x) 2 #This is the Output print(type(x)) (class 'int') #This is the Output #Float x = 0.2 print(x) 0.2 #This is the Output print(type(x)) (class 'float') #This is the Output #String x = "How are you?" #using double quotes print(x) How are you? #This is the Output print(type(x)) (class 'str') #This is the Output y = 'Hello' # using single quotes print(y) Hello #This is the Output print(type(y)) (class 'str') #This is the 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.
Below is the example for string operations in python.
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. Also, the type of the variable ‘a’ is shown as a string.
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 coverting 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.
Code to convert string into an integer in python
print(type(x)) x = int(x) print(x,type(x)) (class 'str') #Output ValueError: invalid literal for int() with base 10: 'Hello'
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 if it contains a number.
float ( ): To convert a number into float format.
Code to conver a number in into float format
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 covert variable to 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 comments if you have any query. We hope you hasd great introduction to python. If you want to start learning R, this article can help you get there.
The Introduction to python is submitted by Jinal Shah.