You are on page 1of 18

python

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

String3 = ''' Python is a simple language"'


print("\nString with the use of Triple Quotes: ")
print(String3)
print(type(String3))
2) List
Lists are just like the arrays, declared in other languages which is a ordered collection of data.
It is very flexible as the items in a list do not need to be of the same type.
Creating List
Lists in Python can be created by just placing the sequence inside the square brackets[].
Example
List = []
print(List)  []
List = [12,45,23,4.5,”hello”]
print(List)  12,45,23,4.5, ‘hello’
Output 12,45,23,4.5,’hello’
print(List[0])  12
print(List[2])  23
print(List[-2])  4.5
print(List[-4])  45
3) Tuple
• Tuple is also an ordered collection of Python objects.
• The only difference between tuple and list is that tuples are immutable i.e. tuples cannot be modified
after it is created. It is represented by tuple class.
Creating Tuple
• In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or without the
use of parentheses for grouping of the data sequence.
• Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.).
Python program to demonstrate creation of tuple

# 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’>

NameError: name 'true' is not defined


Set
• In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate
elements.
• The order of elements in a set is undefined though it may consist of various elements.
Creating Sets
• Sets can be created by using the built-in set() function with an iterable object or a sequence by placing
the sequence inside curly braces, separated by ‘comma’.
• Type of elements in a set need not be the same, various mixed-up data type values can also be passed to
the set.
program to demonstrate Creation of Set in Python

# Creating a Set # Creating a Set with a mixed type of


set1 = set() values(Having numbers and strings)
print("Initial blank Set: ") set1 = set([1, 2, ‘python', 4, ‘class', 6,
‘students'])
print(set1)
print("\nSet with the use of Mixed Values")
print(set1)
# Creating a Set with the use of a String
Output:
set1 = set(“hello python")
Initial blank Set:
print("\nSet with the use of String: ") set()
print(set1) Set with the use of String:
{‘h', ‘e’, ‘o', ‘p’, ‘l’, ‘t', ‘n’, ‘y', 'e}
# Creating a Set with the use of a List Set with the use of List:
set1 = set([“this", “is", “Python"]) {‘this’, ‘is '}
print("\nSet with the use of List: ") Set with the use of Mixed Values
print(set1) {1, 2, 4, 6, ‘this’, ‘is '}
Dictionary
• Dictionary items are ordered, changeable, and does not allow duplicates.
• Dictionary items are presented in key:value pairs, and can be referred to by using the key name.
• Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a
‘comma’.
Creating Dictionary
• In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces,
separated by ‘comma’.
• Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and
must be immutable.
• Dictionary can also be created by the built-in function dict().
• An empty dictionary can be created by just placing it to curly braces{}.
• Dictionary keys are case sensitive,
program to demonstrate Creation of Dictionary in Python

# Creating an empty Dictionary


Dict = {}
print("Empty Dictionary: ")
print(Dict)

# 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

or If one of the expressions is true, then the condition will be true. If a


and b are the two expressions, a → true, b → false => a or b → true.
and If both the expression are true, then the condition will be true. If a
and b are the two expressions, a → true, b → true => a and b → true.

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.

~+- The negation, unary plus, and minus.


* / % // The multiplication, divide, modules, reminder, and floor division.
+- Binary plus, and minus
>> << Left shift. and right shift
& Binary and.
^| Binary xor, and or
<= < > >= Comparison operators (less than, less than equal to, greater than, greater then
equal to).
<> == != Equality operators.
= %= /= //= -= += Assignment operators
*= **=

You might also like