2.fundamentals of Python
2.fundamentals of Python
Home /
Tutorial /
Fundamentals of Python
Fundamentals of Python
By Manoj 8.6 K Views 29 min read Updated on December 30, 2021
In this part of the Python Fundamental tutorial, you will learn about statements like expression statement and assignment
statement, RHS expression, indentation, tokens, keywords, identifiers, various types of operators and lots more.
Python Tutorial
Fundamentals of Python
Python Introduction
What is Python?
Python Version
Python Frameworks
Python Syntax
Comments in Python
Python Variables -
Constant, Global & Static
Variables
Numbers in Python
String in Python
Python Lists
Tuple in Python
Python Sets
Python Dictionary
Python Operators
Type conversion in
Python
Python If Else
Statements
Python Functions -
Define & Call a Functions
in Python
Lambda Function in
Python
Python Built in
Functions with Examples
Python Fundamentals:
Fundamentals of Python consists of a discussion of basic building blocks of the Python programming language. Here,
“Fundamentals of Python” is divided into the following categories. And we will be discussing each topic separately.
Python Course | Learn Python in 11 Hours | Python Tutorial For Beginners | Intellipaat
Statements
Expressions
Assignment Statements
Indentations
Comments
Single-line comments
Multi-line comment
doc-staring comments
Variables
Constants
Tokens
Identifiers
Keywords
Literals
Operators
Statements
Python statements are nothing but logical instructions that interpreters can read and execute. It can be both single and
multiline.
Expression Statements
Assignment Statements
Expression Statement:
With the help of expressions, we perform operations like addition, subtraction, concentration etc.
it is an expression if it appears-
Example:
Using simple arithmetic expression:
(1+5) * 3
18
pow (3,2)
Assignment Statements
With the help of assignment statements, we create new variables, assign values and also change values.
variable = expression
We can categorize Assignment statements into three primary categories based on what’s on the Right-Hand Side of the
statement.
id(test)
Note:
test1="Hello"
id(test1)
output:
2524751071304
test2="Hello"
id(test2)
output:
2524751071304
As you might have noticed that we have assigned the same string to two different variables. But python allocated the
same memory location for both the variables. That is because:
Python allocates the same memory location for the two cases mentioned below:
If the strings with less than 20 characters that don’t have white spaces and
Integers ranging between (-5 to +255).
print(id(current_var))
new_var= current_var
print(id(new_var))
24751106240
2524751106240
As you can see we have the same location id allotted for the two variables.
Operation on RHS:
In this category, we have an operation on the right side of the statement, which is the defining factor of the type of our
statement.
test= 7 * 2
type(test)
int
test1= 7 * 2 / 10
type(test1)
output:
float
Multiline statements
There are two ways to define multiline statements.
Implicit Method
Explicit Method
The end of a statement in python is considered as a newline character, to extend the statements over multiple lines we can
use two methods.
e.g.
a = (0 + 1 + 2 +
3 + 4 + 5)
a = 0 + 1 + 2 + \
3 + 4 + 5
Indentation
Unlike most programming languages python uses indentation to mark a block of code. According to python style
guidelines or PEP8, you should keep an indent size of four.
Most of the programming languages provide indentation for better code formatting and don’t enforce to have it. But in
Python it is mandatory. This is why indentation is so crucial in Python.
Comments
Comments are nothing but tagged lines of in codes which increases the readability of the code and make the code self-
explanatory. Comments can be of two categories:
Single-line comments:
With the help of one ‘#’, we begin a single-line comment
Example:
test= 7 * 2
type(test)
#Single-line comment
Multi-line comments:
With the help of ‘‘‘… ’’,’ we write multiline comments in python.
Example:
test1= 7 * 2 / 10
type(test1)
'''
line one
line two
line three
'''
Docstring comments:
Python has the documentation strings (or docstrings) feature. It gives programmers an easy way of adding quick notes with
every Python module, function, class, and method.
The strings defined using the triple-quotation mark are multiline comments. However, if such a string is placed immediately
after a function or class definition or on top of a module, then they turn into docstrings.
Example:
def SayFunction():
'''
Strings written using '''_''' after a function represents docstring of func
'''
print(theFunction.__doc__)
Variables:
A variable is a memory address that can change and when a memory address cannot change then it is known as constant.
Variable is the name of the memory location where data is stored. Once a variable is stored then space is allocated in
memory. It defines a variable using a combination of numbers, letters, and the underscore character.
i = 1
j = 2
a=2
a, b, c = 2, 25, ‘abc’
Note: Python is a type inferred language i.e. it automatically detects the type of assigned variable.
For instance,
test=1
type(test)
output:
int
test1="String"
type(test1)
output:
str
Constants:
Constant is a type of variable that holds values, whose value cannot be changed. In reality, we rarely use constants in
python.
PI = 3.14
GRAVITY = 9.8
Then import it to the main file.
import constant
print(constant.PI)
print(constant.GRAVITY)
Token
Tokens are the smallest unit of the program. There are the following tokens in Python:
Keywords:
Keywords are nothing but a set of special words, which are reserved by python and have specific meanings. Remember
that we are not allowed to use keywords as variables in python.
help> keywords
Here is a list of the Python keywords. Enter any keyword to get more help.
Identifiers
Identifiers in python are nothing but user-defined names to represent programmable entities like variables, functions,
classes, modules or any other objects.
But there are a few rules that we need to follow while defining an identifier. They are:
You can use a sequence of letters (lowercase (a to z) or uppercase (A to Z)). You can also mix up digits (0 to 9) or an
underscore (_) while defining an identifier.
You can’t use digits to begin an identifier name.
You should not use Reserved Keywords to define an identifier.
Other than underscore (_) you are not allowed to use any other special characters.
Even though python doc says that you can name an identifier with unlimited length. But it is not entirely true.
Using a large name (more than 79 chars) would lead to the violation of a rule set by the PEP-8 standard. It says.
Literals:
The other built-in objects in python are Literals. Literals can be defined as data that is given in a variable or constant.
Python has the following literals:
String Literals:
A string literal is a sequence of characters surrounded by quotes. We can use both single, double or triple quotes for a
string. And, a character literal is a single character surrounded by single or double-quotes.
Numeric Literals:
Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types Integer, Float,
and Complex.
Boolean Literals:
A Boolean literal can have any of the two values: True or False.
Collection literals:
There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.
Special literals:
Python contains one special literal i.e. None. We use it to specify that field that is not created.
Operators:
Operators are the symbols that perform the operation on some values. These values are known as operands.
Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Membership Operators
Identity Operators
Bitwise Operators
Arithmetic Operators
>>>I+ J
>>>60
>>>I – J
>>>20
>>>I * J
>>> 800
>>>I /J
>>> 2.5
>>>I /J
>>> 0
>>>I /J
>>> 204
// Floor Division Perform division remove the decimal value and return Quotient I=30,
value J=20
>>>I//J
>>> 1
Relational Operators
It is also known as the comparison operator because it compares the values. After comparison, it returns the Boolean value
i.e. either true or false.
== Equal to If the values of two operands are equal, then then it returns true. I = 20, J =
20
(I == J) is
True
!= Not Equal to If the values of two operands are not equal, then it returns true. I = 20, J =
20
(I == J) is
False
< Less than If the value of the left operand is less than the value of the right operand, I = 40, J =
then it returns true 20
(I < J) is
False
> Greater than If the value of the left operand is greater than the value of the right I= 40, J =
operand, then it returns true 20
(I > J) is
True
<= Less than or If the value of the left operand is less than or equal to the value of the I = 40, J =
equal to right operand, then it returns true 20
(I <= J) is
False
>= Greater than or If the value of the left operand is greater than or equal to the value of the I = 40, J =
equal to right operand, then it returns true. 20
(I >= J) is
True
<> Not equal to If the values of two operands are not equal, then the condition becomes I=40, J =
(similar to !=) true 20
(I <> J) is
True.
Assignment Operators
= Assignment It assigns a value from right side operand to left side operand I = 40
It assigns 40
to I
+= Add then assign It performs addition and then results is assigned to the left- I+=J
-= Subtract then assign It performs subtraction and then results is assigned to the left- I-=J
*= Multiply the assign It performs multiplication and then the results are assigned to I*=J
%= Modulus then assign It performs modulus and then results is assigned to the left- I%=J
**= Exponent then assign It performs exponent and then results is assigned to the left- I**=J
//= The floor division It performs floor division and then results is assigned to the left- I//=J
Logical Operators
and Logical AND When Both side condition is true the result is true otherwise false 2<1 and
2<3
False
or Logical OR When at least one condition is true then the result is true otherwise 2<1 or 2<3
false True
False
Membership Operators
in It returns true if it finds a variable in the sequence otherwise returns false List = [1,2,3,4,5,6,7,8]
i=1
if i in List:
else:
if j not in List:
else:
Bitwise Operators
J = 20
I = 10 = 0000 1010
J = 20 = 0001 0100
0000 0000
0001 1110
0001 1110
<< Binary Left Shift The left operand is moved left by the number of bits specified by the I << 2
>> Binary Right The left operand is moved right by the number of bits specified by I >> 2
Identity Operators
These operators are used to compare the memory address of two objects.
J = 20
if(I is J):
else:
is not It returns true if both operand‘s identity is not the same otherwise I = 20
false J = 230
else:
This brings us to the end of this module. The next module in Learn Python Online highlights Python datatypes. See you
there!
Further, you can check our offers for the Python Certification course.
Previous Next
Course Schedule
Name Date
2022-05-07 2022-05-08
2022-05-14 2022-05-15
2022-05-21 2022-05-22
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment