Dcsn02c Statements and Operators Intro-To-python
Dcsn02c Statements and Operators Intro-To-python
PRELIMS_FIRSTSEM
Expressions
Assignment Statements
Indentations
Comments
Single-line comments
Multi-line comment
doc-staring comments
Variables
Constants
Tokens
Identifiers
Keywords
Literals
Operators
DCSN02C – CS101
PRELIMS_FIRSTSEM
I. STATEMENTS
1.1 EXPRESSION STATEMENT
- With the help of expressions, we perform operations like addition,
subtraction, multiplication, concentration, etc.
- A statement that returns a value
- It is an operation if it appears –
1. On the right side of the assignment.
2. As a parameter to a method call.
return.
(1+5) * 3
18
pow (3,2)
9
Variable = expression
II. INDENTATION
- Python uses indentation to mark a block of code.
- Python style or PEP8 (indention size of four)
- Programming languages provide indentation for better code
formatting and don’t enforce to have it.
- Indentation is mandatory in Python.
III. COMMENTS
- Nothing but tagged lines of in codes which increase the readability of
the code and make the code self-explanatory.
Two categories:
Single-line comments:
Example:
test = 7 * 2
type(test)
#Single-line comment
Multi-line Comments:
Example:
test1 = 7 * 2 / 10
type(test1)
line one
line two
line three
…
DCSN02C – CS101
PRELIMS_FIRSTSEM
Docstrings comments:
Example:
def SayFunction():
'''
Strings written using '''_''' after a function represents docstring of func
Python docstrings are not comments
'''
print("just a docstring")
print("Let us see how to print the docstring value")
print(theFunction.__doc__)
IV. 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
Multiple Variable Assignment:
a=2
- Also, we can assign multiple values to the multiple variables as
follows-
a, b, c = 2, 25, ‘abc’
DCSN02C – CS101
PRELIMS_FIRSTSEM
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
V. CONSTANTS
VI. TOKEN
Tokens are the smallest unit of the program. There are the following tokens
in Python:
Reserved words or Keywords
Identifiers
Literals
Operators
DCSN02C – CS101
PRELIMS_FIRSTSEM
6.1 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.
Keywords in python are case sensitive.
We’ve just captured here a snapshot of the possible Python keywords.
6.2 IDENTIFIERS
Using a large name (more than 79 chars) would lead to the violation of a rule set
by the PEP-8 standard. It says.
reference for PEP-8 PEP 8 – Style Guide for Python Code | peps.python.org
DCSN02C – CS101
PRELIMS_FIRSTSEM
6.3 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 And, a string in Python. 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.
DCSN02C – CS101
PRELIMS_FIRSTSEM
VII. OPERATORS
Operators are the symbols that perform the operation on some values. These
values are known as operands.
In Python, operators are categorized into the following categories:
Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Membership Operators
Identity Operators
Bitwise Operators
not in It returns true if it does not find a variable in the List = [1,2,3,4,5,6,7,8]
sequence otherwise returns false j=10
if j not in List:
print (‘j is not available in
list’)
else:
print (‘j is available in list’)
Output – j is not available in
list
DCSN02C – CS101
PRELIMS_FIRSTSEM
These operators are used to compare the memory address of two objects.