Python Programming: Introduction to Hello World, Byte Code, Python Virtual Machine, Commenting, Documentation, Variables, and Operators.
When programmers first write a program, they write a very simple Hello World program. We also start here with Hello World.
To start, run the following code in the program window –
1 print("Hello World")
If you run, we
"Hello World"
I will see the text.
Our first Python program is done. In this program we saw how to write and run a Python code.
Here print() is a Python library function. If you put something inside the first parentheses of this function, you will see it in the output.
When you write something inside the print() function, it shows as output. But how it works will be discussed in detail in this episode.
From "running" a program to displaying the output, there are several steps that are usually invisible to us, these steps can be divided into two -
A.Convert to Byte Code.
B. Python Virtual Machine or PVM for short.
•Convert to Byte Code
Python first converts the source code into byte code. In short, it is a medium that converts written programs into suitable machine language. Byte code is machine independent, a completely independent medium that can execute much faster than source code.
We can see the bytecode on our computer or machine whenever we want. The byte code is created in a folder named _pycache_ in the directory where our source file is located. But it saves in .pyc extension instead of .py. pyc means python compiled. But there is a way to name it.
Let's say you write a program and save it as test.py. Then its bytecode can be like this – test.cpython-38.pyc i.e. this Python interpreter is cpython and version 3.8
″Once compiled into byte code, it runs from the byte code until any changes are made to the source code."
•Python Virtual Machine
Python Virtual Machine is Python's runtime engine (Run time Engine) and the last step. This essentially converts the program into an instruction.
Comment and Documentation
Comments or documentation help make any program understandable to everyone. Let's say we write a huge program or build a piece of software. If someone sees that code, it is normal not to understand. So the best practice is to use enough comments in the code, with documentation if possible. Here we will learn about commenting and documentation in Python.
Comment
Commenting in code is usually done to keep things simple, or to write down what a function or variable is used for. A while ago we wrote a code for Hello World, repeat the code here –
print("Hello World")
Since this is a very short piece of code, commenting is fine here. But what if the code is big? Let's write this code using some comments-
#This code is to display "Hello World" in console
print("Hello World")
The above code will show the same result as before. But here a new programmer can easily understand what is done with this program.
To comment a line in Python, you need to place a '#' symbol in front of that line. That is, any statement preceded by # will be considered as a comment and it will not be displayed as output of the main program.
Let's say a few more lines need to be commented, then? We can do this by putting a '#' symbol in front of each line. But there is a simpler solution in Python. Let's take a look at the following code –
"""
Created on Mon Jun 12 18:13:18 2023
@author: MJSWEBDEVELOPMENT
"""
#This code is to display "Hello World" in console
print("Hello World")
The result will be the same "Hello World" as before; But here we have given my name and date in the code, i.e. commented multiple lines. Multiple line comments start with """ and end with """.
KEYWORDS: Single line comment with '#' and multiple line comment with “”” symbol.
Variable in Python
The concept of variable in Python is the same as in other programming languages. A variable is a container.
Let's give an example used in our daily life. We drink water every day; how to do First, pour water from a large pot into a glass and drink water from that glass. Here the water container is the glass and the big pot. In programming language, both the container and the glass are variables, because these two objects hold the water.
Again, with this water glass, we can drink not only water, but also milk or syrup. In the same way multiple operations can be done with one variable, i.e. the ingredients (water, milk, syrup) are changed.
So we understand, a variable is something that holds something else.
(Note: In real life we can drink water and milk in the same glass but in programming we cannot hold multiple objects in the same variable)
So far we have been looking at real life examples. Let's see how it works in programming. Notice the following code –
1 print(10)
Here will be the result – 10
'10' is a number that we pass directly into print() and that is what is shown as the result. Now we can put it in a variable and get the result through print() –
1 a = 10
2 print(a)
Here 'a' is the variable where we put the number '10' and display the result through print() function to this variable.
"Here '=' symbol is called assignment operator in programming language. A value is assigned to a variable using the assignment operator."
We declare some more variables-
1 a = 10
2 b="Hello MJS"
3 print(a)
4 print(b)
The result will be –
10 Hello MJS
That is, the variable can be any type, it can be a "number", a "character", a "string", etc.
Other programming languages require specifying the type of a variable; But Python doesn't need that.
In Python, multiple variables can be assigned simultaneously by commas in the same statement.
For example –
1 a,b=10,"Hello MJS"
2 print(a)
3 print(b)
The result will be the same as before.
Naming variables
Apparently variable names can be anything but there are some general rules. There are certain keywords that cannot be declared as variables. Like – break, for, if etc. These are Python's reserve keywords. Moreover –
•Variable names can never start with a number, must start with a letter or an underscore.
• Variables can only consist of numbers, letters, and underscores.
•Variable names are case-sensitive, meaning that lowercase and uppercase letters indicate different variables. As "a" and "A" are two different variables.
Python operators
We have learned about variables in Python. An operator is something that operates on a variable or a constant, i.e. performs a new operation. We have been familiar with the operator for a long time. For example consider a mathematical statement –
0+10=20
Here' 10 ' is the operand and ' + ' is the operator that operates on the operand. There are several types of operators in Python –
1. Arithmetic operator
2. Assignment operator
3. Comparison operator
4. Logical operator
5. Identity operator
6. Bit-wise operator
7. Membership operator
Let us see them one by one –
Arithmetic operator
You may have understood by now what this operator does by looking at the name. Yes, arithmetic operators are used to solve arithmetic problems.
Operator | Name | Discription |
---|---|---|
+ | Add | Add is used to add two numbers. –Subtract is used to subtract another number from one number. |
- | Subtraction | Subtraction is used to subtract another number from one number. |
* | Multiplication | Multiplication is used to multiply two numbers. |
** | Exponential |
Exponential is used to calculate the power of a number. |
/ | Division | Division is used to divide one number by another number. |
// | Floor division |
Floor division is used to get the quotient as a rational number. The fractional part is ignored in the field. |
% | Modulus |
Modulus The modulus is used to find the quotient after division. |
Now we have seen an example for each operator –
# This program is an example of an arithmetic operator in python.
1 a = 14
2 b=5
3 print (a+b) #add
4 print(a-b) #subtract
5 print(a*b) #multiply
6 print(a**b) #exponential
7 print(a/b) #divide
8 print(a//b) #floor division.
9 print(a%b) #modulas.
Assignment operator
The assignment operator is used to assign a value to a variable. Below are some assignment operators –
Operator | explanation |
Description |
---|---|---|
= | Basically this is the actual assignment operator. All the rest are derived from it. | Assigns the value of the right-hand operand to the left-hand operand. |
+= | a+=b means a=a+b | Adds the left and right operands and assigns the value to the left operand. |
-= | a-=b means a=a-b |
Adds the right operand to the left operand and assigns the value to the left operand. |
* = | a*=b means a=a*b |
Multiplies the left and right values and assigns them to the left operand. |
/ = | a/=b means a=a/b |
Assigns the left operand by dividing the left operand by the value of the right operand. |
%= | a%=b means a=a%b |
Takes the modulus of the two operands and assigns it to the left operand. |
//= | a//=b means a=a//b |
Divides the floor by the left operand and assigns to the left operand. |
** = | a**=b means a=a**b |
Computes the index value and assigns the value to the left-hand operand. |
Note:Here that many expressions can be written much more concisely by using only the '=' operator.
# This program is an example of assignment operator in python
1 a = 10
2 b=5
3 c = 8
4 d=4
5 e = 6
6 f = 7
7 g=9
8 h = 6
i=a # = operator
print (i)
b+=a # += operator
print(b)
c-=a # -= operator
print(c)
d*=c # *= operator
print(d)
e/=d # /= operator
print(e)
f%=e # %= operator
print(f)
g//=f # //= operator
print(g)
h**=g # **= operator
print(h)
Comparison operator
Comparison operator is used to compare two values. Note here that the value of the statement created using this operator is "True" or "False". Check out these operators below –
Operator | Name | Description |
---|---|---|
== | equal | Checks if two operands are equal. Returns True if equal and False otherwise. |
!= | not equal | Just the opposite of the equals operator. That is, returns True if not equal and False if equal. |
> | greater than | Used to check case between two operands. Returns True if the left-hand operand is greater than the right-hand operand, otherwise returns False. |
< | less than | The opposite of the greater than operator, i.e. Returns False if the left operand is greater than the right operand and True otherwise. |
>= | greater than or equal to | Similar to the greater than operator, only here equals will be true. |
< | less than or equal to | Similar to the less than operator, but here equals to true |
Here are some examples using these operators –
# This program is an example of comparison operator in python
1 a = 10
2 b=5
3 print(a==b) # == operator
4 print(a!=b) # != operator
5 print(a>b) # > operator
6 print(a<b) # < operator
7 print(a>=b) # >= operator
8 print(a<=b) # <= operator
Logical operator
Logical operators are generally used to simplify one or more conditional statements to arrive at a general conclusion. There are total 3 logical operators –
Operator | Description |
---|---|
and | Returns True if both statements are true. |
or | Returns True if either statement is true. |
not | shows the opposite of the results obtained. That is, if it is true it is false and if it is false it makes it honest. |
Now we have seen some examples of logical operators-
1 a=5
2 b=10
3 print(a<b and b>a) # and Operator
4 print(a>b or b>a) # or Operator
5 print(not(a>b or b>a)) # not Operator
Identity operator
Earlier we looked at comparison operators; But they are basically working on value. The identity operator is used to check whether an object is the same i.e. belongs to the same memory location.
The number of identity operators is only two –
Oparator | Description |
---|---|
is | Returns True if the two objects are the same. |
is not | Returns True if the two objects are not the same. |
# This program is an example of identity operator in python
1 a = 10
2 b=10
3 c = a
4 print(a is b) # is operator
5 print(a is not b) # is not operator
6 print(a is c) # is operator
7 print(a is not c) # is not operator
Bit-wise operator
Bitwise operators are used for binary calculations. For those who have a good understanding of Logic Gate, this part will be useful to understand.
Although this episode is not meant to discuss logic gates or binary numbers, let's discuss a very small amount of gates. As our computer or any electronic device is based on binary numbers, i.e. 0 or 1, commonly used numbers need to be converted to binary.
For example '5' in binary is '101' and '10' in binary is '1010'. Now if we add '5' and '10' in binary then –
1010
+101
-----
1111
1111 i.e 15
I hope you understand. If you don't understand then you can check it from here. Now we look at the operators –
Oparator | Name | Description |
---|---|---|
& | AND | logical AND operator; ie will be 1 if all bits (Input Line) are 1. |
I | OR | logical OR operator; That is, if any one bit (Input Line) is 1, it will be 1. |
^ | XOR | logical XOR operator; That is, if odd number of bits (Input Line) is 1 then only 1 will be. |
~ | NOT | logical NOT operator; That is, 0 is 1 and 1 is 0. |
<< | Binary left shift | To shift the bits to the left. |
>> | Binary right shift | To move the bits to the right. |
Now let us see some examples of these –
# This program is an example of bitwise operator in python.
1 a = 10
2 b=5
3 print(a&b) # AND operator
4 print(a|b) # OR operator
5 print(a^b) # XORoperator
6 print(~a) # NOT operator
7 print(a<<2) # Binary Left Shift operator.
8 print(a>>2) # Binary Right Shift operator
Note: Here two cells are shifted left and right with a<<2 and a>>2. Value of a is 10 and binary of 10 is 1010;
Moving it two places to the left will be 1010 00 i.e. 40. Again moving it to the right will be 00 10 i.e. 2.
Membership operator
The membership operator is used to check whether a Python object (list, tuple, string, etc.) contains a specific element. Python has two membership operators –
Header 1 | Header 2 |
---|---|
in | Returns True if the element is contained in the specified object. |
not in | Returns True if the element is not contained in the specified object. |
Example :
# This program is an example of membership operator in python.
a = 10
b=5
testList=[1,10,20,30,40,50] # This is a list
print(a in testList) # in operator
print(b in testList) # in operator
print(a not in testList) # not in operator
print(b not in testList) # not in operator
0 Comments