Unit 2 Python
Unit 2 Python
Unit 2 Python
Python provides us the feature to execute the Python statement one by one at the
interactive prompt. It is preferable in the case where we are concerned about the
output of each line of our Python program.
To open the interactive mode, open the terminal (or command prompt) and type
python.
it will open the following prompt where we can execute the Python statement
and check their impact on the console.
The interpreter prompt is best to run the single-line statements of the code.
However, we cannot write the code every-time on the terminal. It is not suitable
to write multiple lines of code.
Using the script mode, we can write multiple lines code into a file which can be
executed later. For this purpose, we need to open an editor like notepad, create a
file named and save it with .py extension, which stands for "Python".
Indentation
Since Python is an infer language that is smart enough to determine the type of
a variable, we do not need to specify its type in Python.
Variable names must begin with a letter or an underscore, but they can be a
group of both letters and digits.
Data Types
Every value has a datatype, and variables can hold values. Python is a
powerfully composed language; consequently, we don't have to characterize the
sort of variable while announcing it. The interpreter binds the value implicitly to
its type. a=5
Python has four basic data types:
• Integer
• Float
• String
• Boolean
Integer
An integer can be thought of as a numeric value without any decimal. In fact, it
is used to describe any whole number in Python such as 7, 256, 1024, etc.
Float
A float stands for floating point number which essentially means a number with
fractional parts. It can also be used for rational numbers, usually ending with a
decimal such as 6.5, 100.1, 123.45, etc.
Boolean
This built-in data type can have one of two values, True or False. We use an
assignment operator = to assign a boolean value to variables in a manner similar
to what we have seen for integer and float values.
String
A string is a collection of alphabets, numbers, and other characters written
within a single quote ' or double quotes ". In other words, it is a sequence of
characters within quotes.
#Program to demonstrate data types
Keywords
Python keywords are unique words reserved with defined meanings and
functions that we can only apply for those functions.
Example:
>>> x -5
Example:
>>> 3 + 10
1. Arithmetic operators
Arithmetic operators are used with numerical values to perform the common
mathematical operations.
Example
print("Arithmetic Operator")
a=10 b=5
print("Addition:",a+b)
print("Subtraction:",a-b)
print("Multiplication:",a*b)
print("Division:",a/b)
print("Floor Division:",a//b)
print("Modulus:",a%b)
print("Exponent",a**b)
2. Relational operator
Example:
print("Relational Operator")
a=10
b=5
print(a>b)
print(a=b)
print(a<=b)
3. Logical operator
• Logical operator are typically used with Boolean(logical) values.
• They allow a program to make a decision based on multiple condition.
Example
print("Logical Operator")
print(10<20) and (10<20))
print(10<5 or 10<20)
print(not(10 <20))
4. Bitwise operator
Bitwise operators act on operands as if they are string of binary digits. It
operates bit by bit.
5. Special operator
• Python offers some special operators like identity operator and the
membership operator.
• Identity Operator: – is and is not are the identity operator
Example
a1=5
b1=5
a2="Hello"
b2="Hello"
a3=[1,2,3]
b3=[1,2,3]
print(a1 is not b1)
print(a2 is b2)
print(a2 is b3)
6. Ternary operator
The ternary operator determines if a condition is true or false and then returns
the appropriate value in accordance with the result. The ternary operator is
useful in cases where we need to assign a value to a variable based on a simple
condition, and we want to keep our code more concise — all in just one line of
code. It’s particularly handy when you want to avoid writing multiple lines for
a simple if-else situation.
Syntax of Ternary Operator in Python
Example:
# Program to demonstrate conditional operator
a, b = 10, 20
print(min)