Professional Documents
Culture Documents
Python print() Function:- The print() function displays the given object to the standard output device
(screen) .
print("hello world")
hello world
Python input() Function:- input() function which is used to take input from the user.
name = input("Enter a name of student:")
print("The student name is: ", name)
Output:
Enter a name of student: Devansh
The student name is: Devansh
By default, the input() function takes the string input but what if we want to take other data types as an
input, we need to typecast the input() function into required datatype.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(a+b)
Data types
Data types are the classification or categorization of data items. It represents the kind of
value that tells what operations can be performed on a particular data.
Following are the standard or built-in data type of Python:
• Numeric
• Sequence Type
• Boolean
• Set
• Dictionary
Numeric data type
Python program to demonstrate numeric value
a=5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
Output:
Type of a: <class 'int'>
Type of b: <class 'float'>
Type of c: <class 'complex'>
Sequence data type
In Python, sequence is the ordered collection of similar or different data types. Sequences
allows to store multiple values in an organized and efficient fashion.
There are several sequence types in Python –
I. String
II. List
III. Tuple
1.)String
• In Python, Strings are arrays of bytes representing Unicode characters.
• A string is a collection of one or more characters put in a single quote, double-quote or
triple quote.
• In python there is no character data type, a character is a string of length one. It is
represented by str class.
Python Program for Creation of String
String1 = 'Welcome to the Python class' name= “python”
print("String with the use of Single Quotes: ") Print(name) python
print(String1) Print(name[4]) h
Print(name[0]) p
String2 = “this is python class"
Print(name[-1]) n
print("\nString with the use of Double Quotes: ")
Print(name[-4]) t
print(String2)
Print(name[1:]) ython
print(type(String2))
Print(name[2:5]) tho
# Creating a tuple
Tuple1 = ()
print (Tuple1) ()
tuple1 = (1,2,3,6.5,”hello”)
print(tuple1) (1,2,3,6.5,’hello’
tuple2= “ a”,”b”,”c”,”d”
Print(tuple) (‘a’,’b’,’c’,’d’)
del(tuple1)
print(tuple)
len(tuple2)
tuple3= tuple1+tuple2
print(tuple)
Boolean
Data type with one of the two built-in values, True or False. But non-Boolean objects can be evaluated in
Boolean context as well and determined to be true or false. It is denoted by the class bool.
Note – True and False with capital ‘T’ and ‘F’ are valid Booleans otherwise python will throw an error.
# Python program to demonstrate Boolean type
print(type(True))
print(type(False))
print(type(true))
Output
<class 'bool'>
<class 'bool’>
# Creating a Dictionary
# with Integer Keys
Dict = {1: “sumit”, 2: “rakesh”, 3: “priya”}
print(Dict)
print(Dict[1])
del(Dict[2])
Print(Dict)
Dict[5]=“puja”
Print(Dict)
Python Operators
Operators are the symbols which perform various operations on Python objects. Python operators are the most
essential to work with the Python data types.
• Arithmetic operators
• Comparison operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Arithmetic operators
** (Exponent) - It is an exponent operator represented as it calculates the first operand power to the second
operand.
Example:- 5 ** 3 or 5 * 5 * 5
// (Floor division)- It gives the floor value of the quotient produced by dividing the two operands.
print (5//2) output -> 2
print (-5//2) output -> -3
Logical Operators
Logical Operators:-The logical operators are used primarily in the expression evaluation to make a
decision.
Operator Description
not If an expression a is true, then not (a) will be false and vice versa.
Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator Description
in Returns True if a sequence with the specified value is present in
the object
not in Returns True if a sequence with the specified value is not present
in the object
Example
x = ["apple", "banana"]
print("banana" in x)
print("pineapple" not in x)
Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the
same object, with the same memory location:
Operator Description
is Returns true if both variables are the same object
is not Returns true if both variables are not the same object
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is y)
print(x is z)
print(x is not y)
Operator Precedence
The precedence of the operators is essential to find out since it enables us to know which operator should
be evaluated first.
Operator Description
** The exponent operator is given priority over all the others used in the expression.