Python Part1
Python Part1
3+5j
10+5.5j
• Note: Complex data type has some inbuilt
attributes to retrieve the real part and
imaginary part
c=10.5+3.6j
c. real==>10.5
c. imag==>3.6
• We can use complex type generally in
scientific Applications and electrical
engineering Applications.
Bool data type
• We can use this data type to represent
boolean values.
• The only allowed values for this data type are:
True and False
• Internally Python represents True as 1 and
False as 0
b=True
type(b) =>bool
Str type
• str represents String data type.
• A String is a sequence of characters enclosed
within single quotes or double quotes.
s1=‘satish‘
s1=“Ramu"
• By using single quotes or double quotes we
cannot represent multi line string literals.
• For this requirement we should go for triple
single quotes(''') or triple double quotes(""")
• We can also use triple quotes to use single
quote or double quote in our String.
• We can embed one string in another string
ex: '''This "Python class very helpful" for java
students'''
• Slicing of Strings:
• slice means a piece [ ] operator is called slice
operator, which can be used to retrieve parts
of String.
• In Python Strings follows zero based index.
The index can be either +ve or -ve.
• +ve index means forward direction from Left
to Right -ve index means backward direction
from Right to Left
Type Casting
• Type Casting: We can convert one type value to
another type. This conversion is called Typecasting or
Type coersion
• The following are various inbuilt functions for type
casting
1.int(): We can use this function to convert
values from other types to int
2. float(): We can use float() function to convert
other type values to float type.
3.complex():We can use complex() function to
convert other types to complex type.
Form-1: complex(x) We can use this function to
convert x into complex number with real part
x and imaginary part 0.
• Form-2: complex(x,y)
• We can use this method to convert x and y
into complex number such that x will be real
part and y will be imaginary part
4. bool(): We can use this function to convert
other type values to bool type
• 5. str(): We can use this method to convert
other type values to str type
• Bytes Data Type: bytes data type represents a
group of byte numbers just like an array.
• Conclusion 1:
The only allowed values for byte data type are 0
to 256. By mistake if we are trying to provide any
other values then we will get value error.
• Conclusion 2:
Once we creates bytes data type value, we
cannot change its values, otherwise we will get
Type Error.
• Bytearray Data type: bytearray is exactly same
as bytes data type except that its elements
can be modified
b[0]=100
• list data type:
• If we want to represent a group of values as a
single entity where insertion order required to
preserve and duplicates are allowed then we
should go for list data type.
Note: An ordered, mutable, heterogenous collection of eleemnts is nothing
but list, where duplicates also allowed.
• tuple data type:
tuple data type is exactly same as list data type
except that it is immutable. i.e we cannot change
values.
Tuple elements can be represented within
parenthesis.
• range Data Type: range Data Type represents a
sequence of numbers. The elements present
in range Data type are not modifiable. i.e
range Data type is immutable
• set Data Type:
If we want to represent a group of values
without duplicates where order is not
important then we should go for set Data
Type.
• frozenset Data Type: It is exactly same as set
except that it is immutable. Hence we cannot
use add or remove functions.
• Dict Data Type: If we want to represent a
group of values as key-value pairs then we
should go for dict data type.
for i in range(2,num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
Program to Check Armstrong Number
# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0