Professional Documents
Culture Documents
1 Data Types
1.0.1 Fundamental Data Types
int = 2, float = 2.4, complex = 1+2j or a+bj, boolean = True or False, String = ‘hello’ or “hello”
10
[2]: int
3.0
[4]: type(b)
[4]: float
[5]: a = 3
print(3)
type(a)
[5]: int
1
1.3 Typecasting - converting from one data type to another data type
[6]: b = 5.9
print(b)
5.9
[7]: type(b)
[7]: float
[8]: int(b)
[8]: 5
[9]: int(2.88888)
[9]: 2
[10]: float(int(4.9999))
[10]: 4.0
True
[12]: type(b1)
[12]: bool
[13]: type(False)
[13]: bool
[14]: int(True)
[14]: 1
[15]: int(False)
[15]: 0
[16]: float(True)
2
[16]: 1.0
[17]: float(False)
[17]: 0.0
[19]: print(x)
(10+20.5j)
[20]: type(x)
[20]: complex
[21]: 10.0
[22]: 20.5
[23]: int(x)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [23], in <cell line: 1>()
----> 1 int(x)
[ ]: int(2+3j)
A complex literal can not be converted to int but the int or float can be converted to complex
[ ]: complex(10)
3
1.6 String
A python string consists of a series or sequence of charecters - letters, numbers and special charecters
We can use single quotes or double quotes to represent the string
[24]: a = 'krishna'
print(a)
krishna
[25]: type(a)
[25]: str
my_string2 = 'Hello'
print(my_string2)
my_string3 = "HELLO"
print(my_string3)
hello
Hello
HELLO
Multiline string can be denoted using triple quotes ”’ ”’ or ””” ”””
[27]: b = '''data science'''
print(b)
data science
[28]: b1 = '''data
science'''
print(b1)
b1
data
science
[28]: 'data\nscience'
[29]: len(b)
[29]: 12
4
[30]: v = 123456
print(v)
type(v)
123456
[30]: int
[31]: v1 = '123456'
print(v1)
type(v1)
123456
[31]: str
[32]: float(v)
[32]: 123456.0
[33]: float(v1)
[33]: 123456.0
[34]: u = 'sri@123'
print(u)
sri@123
[35]: type(u)
[35]: str
[36]: int(u)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [36], in <cell line: 1>()
----> 1 int(u)
5
1.7 Length of String
[37]: string = 'krishna'
len(string)
[37]: 7
[41]: # if we try to access the index which is out of range, then we will get an error
print(string[7])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Input In [41], in <cell line: 2>()
1 # if we try to access the index which is out of range, then we will get␣
↪an error
----> 2 print(string[7])
[42]: print(string[-9])
---------------------------------------------------------------------------
6
IndexError Traceback (most recent call last)
Input In [42], in <cell line: 1>()
----> 1 print(string[-9])
[43]: # string index cannot have a decimal or if we try to access the string index␣
↪with a decimal then we will get an error
print(string[2.4])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [43], in <cell line: 2>()
1 # string index cannot have a decimal or if we try to access the string␣
↪index with a decimal then we will get an error
----> 2 print(string[2.4])
krishna
ish
[47]: print(string[0:3])
kri
[49]: print(string[2:50])
ishna
nhsir
krish
7
[57]: print(string[2:])# default end index - len(string)
ishna
[58]: print(string[::2])
kiha
krishna
anhsirk
[1]: 'helloworld'
• The * operator is used to repeat the string for a given number of times
[4]: 'AI' * 5
[4]: 'AIAIAIAIAI'
[5]: 'krishna' + 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 'krishna' + 2
• to use the + operator for strings, mandatoryly both the literals should be string only
[6]: 'krishna' + '2'
8
[6]: 'krishna2'
[7]: 'krishna3'
[10]: print('congratualtions ' + user + ' you have written ' + lines + ' of code!')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 print('congratualtions ' + user + ' you have written ' + lines + ' of␣
↪code!')
hello
[2]: print(mystring[0])
[4]: mystring[0] = 's' # strings are immutable. it means the values in the string␣
↪indeces can not be replaced
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 mystring[0] = 's'
9
1.12 String Methods
• __strip() - remove spaces from the string
• rstrip() - removes spaces from the right side of the string
• lstrip() - removes spaces from the left side of the string
python
[16]: print(a.strip())
python
[8]: a.strip()
[8]: 'python'
[13]: a.lstrip()
[14]: a.rstrip()
[22]: a.capitalize()# capitalize - only 1st letter of the sentence is in upper case␣
↪remaining all are in the lower case.
[23]: a.title()# title() - makes 1st letter of each word to upper case in each and␣
↪every word in the sentence.
[24]: a.upper() # converts each and every letter in the sentence to upper case
[25]: a.lower() # converts each and every letter in the sentence to lower case
10
[26]: 1
[28]: a.count('o') # to find how many times a string is appeared in a main string
[28]: 3
[1]: 'datascience'
good morning
[2]: 1768842124656
bad morning
[4]: 1768842124080
[6]: c = 'sri123'
print(c.isdigit()) # to find if the string contains only numbers
11
print(c.startswith('s')) # to find if the string starts with the letter 's'
False
True
False
False
False
True
False
False
True
12