Data-Types & Operators in Python
Data-Types & Operators in Python
Numbers
Sequence Type
Boolean
Set
Dictionary
Data types in Python
Data types in Python
Type() function:
To define the values of various data types and check their data types
we use the type() function. Which returns the type of the passed
variable.
Float – This value is represented by the float class. It is a real number with
a floating-point representation. It is specified by a decimal point.
Optionally, the character e or E followed by a positive or negative integer
may be appended to specify scientific notation.
Example:
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’>
String:
Python Strings are identified as a contiguous set of characters represented
in the quotation marks. Python allows for either pairs of single or double
quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] )
with indexes starting at 0 in the beginning of the string and working their
way from -1 at the end.
The plus (+) sign is the string concatenation operator and the asterisk (*) is
the repetition operator in Python.
str = 'Hello World!'
print (str) # Prints complete string
print (str[0]) # Prints first character of the string Output:
Hello World!
print (str[2:5]) # Prints characters starting from 3rd to 4th H
print (str[2:]) # Prints string starting from 3rd character llo
print (str * 2) # Prints string two times llo World!
Hello World!Hello
print (str + "TEST") # Prints concatenated string
World!
Hello World!TEST
Data types in Python
List Example:
list = [ 'abcd', 786 , 2.23, ‘RKS', 70.2 ]
tinylist = [123, ' RKS ']
print (list) # Prints complete list
print (list[0]) # Prints first element of the list
print (list[1:3]) # Prints elements starting from 1st till 2nd
print (list[2:]) # Prints elements starting from 3rd element
print (tinylist * 2)
print (list + tinylist) Output:
['abcd', 786, 2.23, ‘RKS', 70.2]
abcd
[786, 2.23]
[2.23, ‘RKS', 70.2]
[123, ' RKS ', 123, ' RKS ']
['abcd', 786, 2.23, ' RKS ', 70.2, 123, 'RKS ']
Data types in Python
Example:
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("Original list ", List) Output:
Python Dictionary
Python dictionaries are kind of hash table type. They work like associative arrays or
hashes found in Perl and consist of key-value pairs. A dictionary key can be almost
any Python type, but are usually numbers or strings. Values can be any arbitrary
Python object. Dictionaries are enclosed by curly braces ({ }) and values can be
assigned and accessed using square braces ([]).
Output:
This is one
Example: This is two
dict = {} {'dept': 'CSA', 'code': 9020, 'name': 'RKS'}
dict['one'] = "This is one" ['dept', 'code', 'name']
dict[2] = "This is two" ['CSA', 9020, 'RKS']
tinydict = {'name': 'RKS','code':9020, 'dept': 'CSA'}
print (dict['one'])
print (dict[2])
print (tinydict)
print (tinydict.keys())
print (tinydict.values())
Note: Python dictionaries have no concept of order among elements. Simply, they are
unordered.
Data types in Python
Examples
a = True
# display the value of a Output:
print(a) true
# display the data type of a <class 'bool'>
print(type(a))
Data types in Python
Example:
a=2
b=4
print(bool(a==b))
print(a==b)
Output:
a = None
False
print(bool(a)) False
a = () False
print(bool(a)) False
False
a = 0.0 True
print(bool(a))
a = 10
print(bool(a))
Data types in Python
Example:
set = {"apple", "banana", "cherry"}
print(set)
Data types in Python
Example
Duplicate values will be ignored:
Example:
int_num = 123 Output:
float_num = 1.23 Value: 124.23
new_num = int_num + float_num Data Type: <class 'float'>
# display new value and resulting data type
print("Value:",new_num)
print("Data Type:", type(new_num))
Example:
num_string = '12'
num_integer = 23
print("Data type of num_string before Type Casting:",type(num_string))
print("Sum:",num_sum)
print("Data type of num_sum:",type(num_sum))
Type Conversion in Python
Output:
Data type of num_string before Type Casting:
<class 'str'>
Data type of num_string after Type Casting:
<class 'int'>
Sum: 35
Data type of num_sum: <class 'int'>
Key Points:
Type Conversion is the conversion of an object from one data type to
another data type.
Implicit Type Conversion is automatically performed by the Python
interpreter.
Python avoids the loss of data in Implicit Type Conversion.
Explicit Type Conversion is also called Type Casting, the data types of
objects are converted using predefined functions by the user.
In Type Casting, loss of data may occur as we enforce the object to a
Python Operators
Operators in Python
In Python, operators are special symbols or keywords that carry out
operations on values and python variables. They serve as a basis for
expressions, which are used to modify data and execute computations.
Python contains several operators, each with its unique purpose.
Example:
a=4
b=5
# Equal
print ("a == b : ", a == b) Output
a == b : False
# Not Equal a != b : True
print ("a != b : ", a != b) a > b : False
# Greater Than a < b : True
a >= b : False
print ("a > b : ", a > b)
a <= b : True
# Less Than
print ("a < b : ", a < b)
# Greater Than or Equal to
print ("a >= b : ", a >= b)
# Less Than or Equal to
print ("a <= b : ", a <= b)
Operators in Python
# Assignment Operator
a = 10
# Addition Assignment
a += 5
print ("a += 5 : ", a)
# Subtraction Assignment
a -= 5
print ("a -= 5 : ", a)
# Multiplication Assignment
a *= 5
print ("a *= 5 : ", a)
# Division Assignment
a /= 5
Output
print ("a /= 5 : ",a)
a += 5 : 15
# Remainder Assignment
a %= 3
a -= 5 : 10
print ("a %= 3 : ", a) a *= 5 : 50
# Exponent Assignment a /= 5 : 10.0
a **= 2 a %= 3 : 1.0
print ("a **= 2 : ", a) a **= 2 : 1.0
# Floor Division Assignment a //= 3 : 0.0
a //= 3
Operators in Python
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
# Binary AND
c = a & b # 12 = 0000 1100
print ("a & b : ", c)
# Binary OR
c = a | b # 61 = 0011 1101
print ("a | b : ", c)
# Binary XOR
c = a ^ b # 49 = 0011 0001
print ("a ^ b : ", c)
Output
# Binary Ones Complement
a & b : 12
c = ~a; # -61 = 1100 0011
a | b : 61
a ^ b : 49
print ("~a : ", c)
~a : -61
# Binary Left Shift
a >> 2 : 240
c = a << 2; # 240 = 1111 0000
a >> 2 : 15
print ("a << 2 : ", c)
# Binary Right Shift
c = a >> 2; # 15 = 0000 1111
print ("a >> 2 : ", c)
Operators in Python
Example:
x=5 Output
y = 10 Both x and y are within the specified range
if x > 3 and y < 15:
print("Both x and y are within the specified range")
Operators in Python
Example:
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("Yes, banana is a fruit!") Output
else: Yes, banana is a fruit!
print("No, banana is not a fruit!")
Operators in Python
Example:
x = 10
y=5
if x is y:
print("x and y are the same object")
Output
else:
x and y are not the same object
print("x and y are not the same object")
Operators in Python