Class8 Strings List DataType Combine
Class8 Strings List DataType Combine
10 + 20
30
Types of cells
1 - Code cell
2 - Mark down cell
print("Hello")
Hello
10+20
10+20
30
cell 1
cell 2
cell 3
cell 4
cell 5
Heading1
Heading2
Heading3
Heading4
- This is python class
Python
• Point1
– Subpoint1
– Subpoint2
• Point2
• point1
– Subpoint1
– Subpoint2
Programming Language
• Programming Language is the way of communication between Humans and Machines
Python
• Python is a, High level , general purpose, Dynamically typed Programming language
Levels of programming languages
1 - High level -----> More understandable by Humans ----> ex:- Python,
Scala, Java , C++,R ,JS, C#
2 - Low level -----> More understandable by Machines-----> ex:-
Assembly language
3 - Middle level language -----> It is understandable by humans as
well as Machines ----> ex :- C
High level
embedded C
10 + 50
60
NC ,
CNC
print("Hello")
Hello
int a
a = 50
a = 50
int a
a = 50
a = "X"
b = 30.333333
c= 10
'X'
b
30.333333
10
a = 10
b = 20
# c = 30
d = 40
10
20
----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
Cell In[6], line 1
----> 1 c
40
a = 10
b = 20
c = 30
d = 40
print(a)
print(d)
print(c)
print(b)
10
40
30
20
Why Python ?
- Easy to learn or understand
- Open Source
- Vast Libraries ( numpy , Pandas , Scikit lear Scipy , Django , flask
Streamlit....)
- OOPs
- Multi purpose
- Community Support
- can connect to DBs
- Testing
- Cross Platform compatability Mutiple OS (Window , Mac IOS , Linux)
a = 10
var = 60
a = 10
b = 20
c = a+b
print(c)
30
pwd
import sys
sys.path[0]
numpy
Literals
• literals are the values that you see in python
• int
• float
• str
• complex
• bool
• None
a = 10
b = "Hello"
c = 44.777
Constants
• the values tha can not be changed mathematically
• python - Use UPPER case variables to represent the constants
PI = 3.14 # do not change the value as it is a Constant
G = 9.81 # gravity constant do not change it
PI
3.14
pi = 3.14
PI = 500
PI
500
ABC = 55
abc = 98
abc
98
Java script
const a = 500;
gravity_constant
# identifiers/variable
cdf = 500
cdf -----> variable
500 -----> literal
abc = 700
ABC = 900
abc
700
ABC
900
PTDDYython = 99
PTDDYython
99
lmn123 = 55.8888
lmn123
55.8888
_ = 100
_
100
lmslmslms = 99
lmslmslms
99
7086989687 = 90
var70 = 90
var70
90
abc123 = 80000
342abc = 900000
$% = 543
@ = 0
_ = 40
_
40
my var = 200
my_var = 200
LMN = 99
lmn = 77
LMN
99
A = 100
a = 60
60
print = 90
90
print("Hello")
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[35], line 1
----> 1 print("Hello")
for
if
import keyword
dir(keyword)
['__all__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'iskeyword',
'issoftkeyword',
'kwlist',
'softkwlist']
print(keyword.kwlist)
None = 100
print(dir(__builtins__))
b = 700 + 99 # expression
Block
few lines of code together are called block
# indentation - space
space after the condition(:) in between is indentation
a = 100
b = 500
if a==100:
print("Begining of the block")
print("a value is 100")
print("b value is 500")
print("End of the block")
print("Out of the block")
a = 100
b = 500
if a==200:
print("Begining of the block")
print("a value is 100")
print("b value is 500")
print("End of the block")
print("Out of the block")
if(i=0;i<=10;i++)
{codeline1
code2
code
}
for i in lst:
print()
def fun():
code
Comments (#)
• Comments are lines of code to write the explaination
# Why comments?
- Explain the code
- Makes code more redable
- Prevent the execution of the unnecessary code lines
----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
Cell In[19], line 1
----> 1 d
"""
This is a
Multiline Comment
We are writing
in multiline
comments
"""
a = 10
10
int
• integers are positive or negative numbers without decimal points (whole numbers)
a = 99
99
b = 0
b
type(a)
int
type(b)
int
c = -99999
type(c)
int
float
• floating point numbers are positive or negative numbers with decimal points
var = 99.001
var
99.001
type(var)
float
f = -60000.00000
type(f)
float
print(type(f))
<class 'float'>
operations on numbers
# Arithmetic operations
(+)-----> addition
(-)-----> Sbtraction
(*)-----> multiplication
(/)-----> float division (it gives decimal value)
(//)-----> floor division( it gives int value)
(%)-----> modulus
(**)-----> exponent
a = 10
b = 20
# addition
10 + 20
30
a + b
30
c = a + b
c
30
# Subtraction
10 - 20
-10
a -b
-10
b - a
10
# Multiplication (*)
a * b
200
a = 14
b = 3
Float division
10/5
2.0
# floor division
10//5
14/3
4.666666666666667
floor value = 4
2.234358
floor = 2
ceil = 3
import math
math.ceil(4.666666666666667)
round(4.666666666666667)
math.ceil(4.666666666666667 , 2)
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[55], line 1
----> 1 math.ceil(4.666666666666667 , 2)
round(4.666666666666667 , 2)
4.67
# % modulus
Remainder
14%3
10%5
0
# power/ Exponent (**)
4**10
64
4*4*4
64
pwd
type()
math.ceil(-4.88)
5
Comparision operation
- Comparision operators return Only a boolean value (True or False)
< # less than
<= # lessthan or equals
> # greater than
>= # greater than or equals
a = 10
b = 99
10
99
a < b
10 < 99
True
b < a
False
False
True
10
99
a > b
False
b > a
True
False
True
Equality operation
# Equality operators return Only a boolean value (True or False)
== # equals
!= # Not equals
1000 == 1000
True
200 == 400
False
10
99
a == b
False
a != b
True
str1 = "Hello"
str2 = "hello"
str1
'Hello'
str2
'hello'
str1 != str2
True
str3 = "hello"
str4 = "hello"
str3
'hello'
str4
'hello'
str3 == str4
True
72
104
False
"hello"
"hello"
True
"A"
type()
65
99
print(chr(99))
ord("s")
115
Logical operators
True ----> 1
False ---> 0
and
or
not
and
False and False
False and True
True and False
True and True
False
False
False
True
a = 100
b = 500
if a==100 and b==500: # True
print("Begining of the block")
print("a value is 100")
print("b value is 500")
print("End of the block")
print("Out of the block")
True
a = 100
b = 500
if a==100 and b==99: # False
print("Begining of the block")
print("a value is 100")
print("b value is 500")
print("End of the block")
print("Out of the block")
False
or
False or False
False or True
True or False
True or True
False or False
False
False or True
True
True or False
True
True or True
True
not
not True
False
not False
True
a = 5
not a > 3
False
a > 3
True
not True
False
Assignment Operation
= # assignment operator
a = 500
500
Compound operators
i++ Increment operator
i-- Decrement operator
a = 5
a = a+1 # a+=1
5+1
a = a+1 # a+=1
a = a-5 # a-=5
a
a+=8
10
a = 5
a = a+5 # a+=5
a = a-5 # a-=5
a = a*5 # a*=5
a = a/5 # a/=5
a = a//5 # a//=5
a = a%5 # a%=5
a = a**5 # a**=5
Identity operator
# it will check the addresses
# It will retur True or False only
- is
- is not
a = 10
b = 20
c = 30
print(a)
print(b)
print(c)
print(d)
10
20
30
----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
Cell In[64], line 7
5 print(b)
6 print(c)
----> 7 print(d)
----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
Cell In[65], line 1
----> 1 d
d =
id(a)
140733536545496
id(b)
140733536545496
var1 = 550
var2 = 550
id(var1)
2714025093488
id(var2)
2714025090928
var1 = -1
var2 = -1
print(id(var1))
print(id(var2))
140733536545144
140733536545144
var1 = 256
var2 = 256
print(id(var1))
print(id(var2))
140733536553368
140733536553368
2714025089776
2714025091408
a = True
b = True
print(id(a))
print(id(b))
140733535419264
140733535419264
a = None
b = None
print(id(a))
print(id(b))
140733535500240
140733535500240
a = 50
b = 50
print(id(a))
print(id(b))
140733536546776
140733536546776
a is b
True
var1
256
var2
256
abc = 1.002
lmn = 1.002
print(id(abc))
print(id(lmn))
2714004205104
2714025093232
abc is lmn
False
True
a = 50
b = 50
print(id(a))
print(id(b))
140733536546776
140733536546776
False
address_a = id(a)
address_b = id(b)
print(address_a is address_b)
False
print(140733536546776 is 140733536546776)
True
<>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
<>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
C:\Users\Lenovo\AppData\Local\Temp\ipykernel_3816\3745895525.py:1:
SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
print(140733536546776 is 140733536546776)
id(140733536546776)
2714025092720
id(140733536546776)
2714025092528
a=5
b=10
Complex Number
any number that can be represented in the form of a+bj
where
a -----> Real number
b -----> Imaginary number
c = 5+4j
type(c)
complex
d = 99-100J
type(d)
complex
d.real
99.0
d.imag
-100.0
----------------------------------------------------------------------
-----
NameError Traceback (most recent call
last)
Cell In[10], line 1
----> 1 j**2==1
# can no perform
Comparision operation
Arithmetic operation(% modulus operation and floor division)
a = 40 +39J
b = 5+1J
a
(40+39j)
(5+1j)
a + b
(45+40j)
a > b
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[15], line 1
----> 1 a > b
a = 40 +39J
c = 40 +39J
print(id(a))
print(id(c))
1710943534800
1710943533552
a is c
False
a is not c
True
a % c
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[21], line 1
----> 1 a % c
a // b
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[23], line 1
----> 1 a //b
10+5J == 10+5j
True
None Type
• None is used to represent Missing values
a = None
type(a)
NoneType
Boolean data
True (1)
False (0)
1+1
True + True
1 + True
1 + False
int(True)
1
int(False)
False
False
Type Casting
• Changing one data type to other data type is type casting
a = 5.008653
type(a)
float
int(a)
5.008653
float_to_int = int(a)
float_to_int
str1 = "2.23345"
str1
'2.23345'
type(str1)
str
int(str1)
----------------------------------------------------------------------
-----
ValueError Traceback (most recent call
last)
Cell In[45], line 1
----> 1 int(str1)
ValueError: invalid literal for int() with base 10: '2.23345'
str1
'2.23345'
int(float(str1))
int(float(str1))
str2 = "22"
str_to_int = int(str2)
str_to_int
22
type(str_to_int)
int
str2 = "abc22"
str_to_int = int(str2)
str_to_int
----------------------------------------------------------------------
-----
ValueError Traceback (most recent call
last)
Cell In[54], line 2
1 str2 = "abc22"
----> 2 str_to_int = int(str2)
3 str_to_int
a = 10
float(a)
10.0
complex(a)
(10+0j)
bool(a)
True
bool(None)
False
bool(700)
True
bool("Hello")
True
bool("")
False
a = 40 +39J
str(a)
----------------------------------------------------------------------
-----
ValueError Traceback (most recent call
last)
Cell In[74], line 2
1 a = 40 +39J
----> 2 float(str(a))
a.real
40.0
int(a.real)
40
Strings
string is a data type
a
'Hello we are in python class'
type(a)
str
W_S = ''' William Shakespeare[a] (c. 23[b] April 1564 – 23 April 1616)
[c] was an English playwright, poet and actor.
He is widely regarded as the greatest writer in the English language
and the world's pre-eminent dramatist.
He is often called England's national poet and the "Bard of Avon" (or
simply "the Bard"). His extant works,
including collaborations, consist of some 39 plays, 154 sonnets, three
long narrative poems and a few other verses,
some of uncertain authorship. His plays have been translated into
every major living language and are performed more often than
those of any other playwright. Shakespeare remains
arguably the most influential writer in the English language, and his
works continue to be studied and reinterpreted.'''
print(W_S)
W_S = """ William Shakespeare[a] (c. 23[b] April 1564 – 23 April 1616)
[c] was an English playwright, poet and actor.
He is widely regarded as the greatest writer in the English language
and the world's pre-eminent dramatist.
He is often called England's national poet and the "Bard of Avon" (or
simply "the Bard"). His extant works,
including collaborations, consist of some 39 plays, 154 sonnets, three
long narrative poems and a few other verses,
some of uncertain authorship. His plays have been translated into
every major living language and are performed more often than
those of any other playwright. Shakespeare remains
arguably the most influential writer in the English language, and his
works continue to be studied and reinterpreted."""
print(W_S)
type(W_S)
str
W_S
' William Shakespeare[a] (c. 23[b] April 1564 – 23 April 1616)[c] was
an English playwright, poet and actor.\nHe is widely regarded as the
greatest writer in the English language and the world\'s pre-eminent
dramatist.\nHe is often called England\'s national poet and the "Bard
of Avon" (or simply "the Bard"). His extant works,\nincluding
collaborations, consist of some 39 plays, 154 sonnets, three long
narrative poems and a few other verses,\nsome of uncertain authorship.
His plays have been translated into every major living language and
are performed more often than \nthose of any other playwright.
Shakespeare remains\narguably the most influential writer in the
English language, and his works continue to be studied and
reinterpreted.'
a = 'Hello we are in
pythons class'
a = '''Hello we are in
pythons class'''
print(a)
Hello we are in
pythons class
a = 10
b = 20
a+b
30
input()
- it is a function to get the user input from the end user
a = input("Enter the first number ")
b = input("Enter the Second number ")
type(a)
str
type(b)
str
# type casting
a = input("Enter the first number ")
b = input("Enter the Second number ")
a = int(a)
b = int(b)
a+b
40
a + b
'7030'
"Hello" + "World"
'HelloWorld'
# type casting
a = float(input("Enter the first number "))
b = float(input("Enter the Second number "))
a+b
50.0
a
20.0
30.0
10
10.0
eval
print(type(a))
print(type(b))
<class 'str'>
<class 'complex'>
<class 'int'>
<class 'float'>
<class 'str'>
<class 'str'>
Output Function
print()
help(print)
sep
string inserted between values, default a space.
end
string appended after the last value, default a newline.
file
a file-like object (stream); defaults to the current sys.stdout.
flush
whether to forcibly flush the stream.
print("hello")
hello
print(20000)
20000
a = 10
b = 20
c = 30
print(a , end="$$$")
print(b)
print(c)
10
20
30
'\n"
New line
10$$$20
30
a = 10
b = 20
c = 30
print(a , end="@@@\n")
print(b)
print(c)
10@@@
20
30
10@@@
20
30
a = 10
b = 20
c = 30
print(a,b,c , sep=" ")
10 20 30
a = 10
b = 20
c = 30
print(a,b,c , sep="-----------------")
10-----------------20-----------------30
a = 10
b = 20
c = 30
print(a,b,c , sep="\n")
10
20
30
10
20
30
a = 10
b = 20
c = 30
print(a )
print(b)
print(c)
print
print
print
print
print
print
print
print
a = 10
b = 20
c = 30
print(a,b,c , sep="\n")
print(a)
10
20
30
10
format String
first_name = "Jhon"
last_name = "Usman"
# "My first name is Mohammed and my last name is Usman"
first_name = "Mohammed"
last_name = "Usman"
print(f"My first name is {first_name} and my last name is
{last_name}")
Entered number one is 55 and the second number is 60 and the sum both
the numbers is 115
# 2- way .format()
Entered number one is 10 and the second number is 20 and the sum both
the numbers is 30
balance = 5000
debit = int(input("Enter the amount you want to withdraw: "))
final_balance = balance-debit
print(f"You have withdrawn {debit} and the remaining balance is
{final_balance}")
balance = 5000
debit = int(input("Enter the amount you want to withdraw: "))
print(f"You have withdrawn {debit} and the remaning balance is
{balance-debit}")
balance = 5000
debit = int(input("Enter the amount you want to withdraw: "))
final_balance = balance-debit
print(f"You have withdrawn {debit} and the remaning balance is
{final_balance}")
String Properties
• Strings are sequence data types , which means indexing and slicing is possible
a = "python"
b = "cython"
2575070231792
2575075316128
a is b
False
1 - Positive indexing
- positive indexing starts from 0 and it goes from L to R
2 - Negative Indexing
- negative indexing starts from -1 and it goes from R to L
a = "python"
syntax
var_name[index_number]
'o'
# t
a[2]
't'
a[-4]
't'
a = "python"
b = "cython"
a[3]
'h'
b[3]
'h'
id(a[3])
140733553581968
id(b[-3])
140733553581968
Slicing
1- Positive Slicing
2 - Negative Slicing
a[-12:-1:-1]
''
a = "lionel messi"
syntax
var_name[start_index:stop_index:step]
stop----> exclusive
# ion
# +ve
a[1:4:]
'ion'
# mess
a[7:11:]
'mess'
# messi
a[7:12:]
'messi'
a[7::]
'messi'
# -ve Slicing
var_name[start_index:stop_index:step]
a[-10:-7:]
'one'
# lion
# +ve
a[0:4:]
'lion'
# -ve
a[-12:-8:]
'lion'
# messi
a[-5::]
'messi'
# step
a[::]
'lionel messi'
start ----> 0
stop ---> end of the str
step-----> +1 ( L to R)
a[-12:-8:1]
'lion'
a[-12:-8:1]
'lion'
a[-12:-8:-1]
''
a[2:6:1]
'onel'
a[2:6:-1]
a[::]
'lionel messi'
a[0:12:2]
'loe es'
# reverse a string
a[11::-1]
'issem lenoil'
a[::-1]
'issem lenoil'
a[::-2]
'ismlni'
a[0:12:3]
'ln s'
a[::]
'lionel messi'
a[::-1]
balance=500
debit=int(input("enter the amount you want to withdraw: "))
final_balance = balance-debit
print(f"you have withdraw{debit} and the remaining balanceis
{final_balance}")
a = "Wednesday"
"Sednesday"
a[6::] = "way"
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[46], line 1
----> 1 a[6::] = "way"
a[0] = "S"
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[49], line 1
----> 1 a[0] = "S"
100
l[0] = 9000
String Operations
# Indexing
# Slicing
# Concatination
# Identity
# Membership Operation
Concatination (+)
• joining two or more strings is concatination
a = "We are"
b = " Learning "
c = "Python"
a+b+c
a = "Wednesday"
b = "Wednesday"
print(id(a))
print(id(b))
1735102526960
1735102526960
a is b
True
id(a) is id(b)
a is not b
False
str1= "Hello"
str2 = "Hello"
str1 is str2
True
addr_str1 = id(str1)
addr_str1
1735150816880
addr_str2 = id(str2)
addr_str1 is addr_str2
False
Membership Operation (in , not in)
str1 = "Usman loves to play football"
str1
"football" in str1
True
"Cricket" in str1
False
True
"l" in str1
True
"F" in str1
False
string methods
Methods are functions that are defined in a class
'Wednesday'
print(type(a))
<class 'str'>
print(dir(str))
# capitalize()
s = "this is python"
'this is python'
s.capitalize()
'This is python'
'this is python'
# upper()
s = "this is python"
s_upper_case = s.upper()
s_upper_case
'THIS IS PYTHON'
# lower()
s = 'THIS IS PYTHON'
s.lower()
'this is python'
# swapcase()
lower to upper
upper to lower
"apple BANANA".swapcase()
'APPLE banana'
startswith()
s = "Python is a programming language"
s.startswith("P")
True
s.startswith("Z")
False
s.startswith("Python")
True
# endswith()
s.endswith("Z")
False
s.endswith("e")
True
s.endswith("guage")
True
isalpha
isnumeric
isalnum
True
False
False
False
False
False
False
True
True
True
False
True
False
# isascii
# isalnum - True for if the string is having an ASCII value false
otherwise
s1 = "abc" # True
s2 = "123abc" # True
s3 = "$$$$" # True
s4 = "121323" # True
s5 = "हिंदी" # False
print(s1.isascii())
print(s2.isascii())
print(s3.isascii())
print(s4.isascii())
print(s5.isascii())
True
True
True
True
False
# islower()
s = "hello"
s.islower()
True
s = "Hello"
s.islower()
False
# isupper()
s = "hello"
s.isupper()
False
s = "HELLO"
s.isupper()
True
strip
rstrip
lstrip
strip()
s.strip()
'Welcome'
s = "************Welcome**************"
s.strip("*")
'Welcome'
# rstrip()
s = " Welcome "
s.rstrip()
' Welcome'
s = "************Welcome**************"
s.rstrip("*")
'************Welcome'
# lstrip()
s = " Welcome "
s.lstrip()
'Welcome '
s = "************Welcome**************"
s.lstrip("*")
'Welcome**************'
' Password'
"Welcome".strip("Wle")
'com'
find
rfind
index
rindex
22
s = "nUsman"
s.find("n")
s = "nUsnman"
s.rfind("n")
6
# index() - it give the index number of first occurance of the
specified char or group of char from L to R
s = "nUsnman"
s.rindex("n")
s = "Football"
s.find("Z")
-1
s.index("Z")
----------------------------------------------------------------------
-----
ValueError Traceback (most recent call
last)
Cell In[111], line 1
----> 1 s.index("Z")
ljust - leftadjustment
rjust - rightadjustment
Indian
a = "Indian"
a.ljust(30)
'Indian '
30
6
24
a.ljust(30 , "*")
'Indian************************'
a.rjust(30)
' Indian'
split()
join()
s.split()
s = "Usman@loves@to@play@chess"
s.split("@")
l = s.split("@")
l
" ".join(l)
"@".join(l)
'Usman@loves@to@play@chess'
print(list(s))
['U', 's', 'm', 'a', 'n', '@', 'l', 'o', 'v', 'e', 's', '@', 't', 'o',
'@', 'p', 'l', 'a', 'y', '@', 'c', 'h', 'e', 's', 's']
replace
count
count()
new_str
new_str.count("o")
a = "hello" # Immutable
b = [500 , 600 ,300] # mutable
a[0]
'h'
a[0] = "D"
----------------------------------------------------------------------
-----
TypeError Traceback (most recent call
last)
Cell In[141], line 1
----> 1 a[0] = "D"
b[0]
500
b[0] = -1000
s = "Usman@loves@,to@play@chess"
s.split("@,")
['Usman@loves', 'to@play@chess']
s
'I love to play football'
" ".join(l)
a = "lionel messi"
a[1: :-1]
'il'
a[1: :1]
'ionel messi'
print(dir(str))
"abC".swapcase()
help(str.swapcase)
Help on method_descriptor:
print("Hello\nWorld")
Hello
World
Hello
World
# backslash ---> \
"Hello\nWorld"
"Hello'\n'World"
print("Hello\nWorld")
Hello
World
print("Hello\\nWorld")
Hello\nWorld
a = 10
b = 20
print(a)
c = 40
print(c)
10
40
# \b ----> backspace
print("pqr\b\bstuv")
pstuv
pqstuv
print("Hello\rIndian")
Indian
H -----> I
e -----> n
l -----> d
l -----> i
o -----> a
n
print("Indian\rHello")
Hellon
I-----> H
n----->e
d----->l
i----->l
a----->o
n
print("Indian\rHello")
len("Indian")
len("Hello")
print("India\rHello")
Hello
print("Madhangi\r prakash")
# prakash
prakash
len("Madhangi")
len("prakash")
# \t -----> tab
print("Java\tPython")
Java Python
Hello
World
We are learning
Python
32
len("chandana")
s.count("a")
ls
pwd
list
• list is a derived datatype
• homogenous or hetrogenous data in [] is a list
lst = ["Hello" , 1,2,3,4,5]
lst
['Hello', 1, 2, 3, 4, 5]
type(lst)
list
Creating a list
# 1 you know the elements already
lst1
lst2
1000
# 3 - Type Casting
lst3 =list(str1)
lst3
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
# 4 - split()
lst4 = str1.split()
lst4
['Hello', 'World']
Properties of list
• list is a sequence data type , indexing and slicing is allowed
• Mutable
• It can contain homogenous or hetrogenous data or both
• it allows duplicate
• operations -----> Concatination , Repetition , Identity, Membership
lst = [100,200,300,700,900 ,500]
[]
# Indexing
- +ve
- -ve
# extract 700
lst[3]
700
lst[-3]
700
lst[4]
900
lst[-2]
900
# slicing
[700,800,900,300,100]
lst[-1: :-2]
lst[8::-2]
# Mutablility
lst = [100, 200, 300, 700, 900, 500, 800, 600, 700]
lst[5] = 10000
lst
[100, 200, 300, 700, 900, 10000, 800, 600, 700]
lst_homo = [100, 200, 300, 700, 900, 500, 800, 600, 700]
# --- int ------> homgenous data
lst_homo
lst_hetro
lst_dup
operations on list
# Concatination , Repetition , Identity, Membership
(+)
lst
lst
print(lst + lst)
[100, 200, 2.2, (3+7j), 'Hello World', 600, 700, 100, 200, 2.2,
(3+7j), 'Hello World', 600, 700]
[1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
Repetition (*)
[1,2,3] * 5
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
"Hello" * 3
'HelloHelloHello'
# Identity
lst1 = [1,2,3,4]
lst2 = [1,2,3,4]
print(id(lst1))
2231367745216
print(id(lst2))
2231347778240
id(lst1[0])
140733550307768
id(lst2[0])
140733550307768
lst1 is lst2
False
lst1[0] is lst2[0]
True
False
lst
900 in lst
True
True
"3.55" in lst
False
3.55 in lst
True
nested list
# list inside a list is a nested list
nested_list[2]
400
nested_list[3]
[5, 4, 9, 0.7]
nested_list[3][3]
0.7
"Avengers"
print(nested_list)
[100, [200], 400, [99, [487, 88, 76, ['Thor', 'Hulk', 86, [55, 77, 64,
'Avengers']]]], [5, 4, 9, 0.7]]
nested_list[3]
[99, [487, 88, 76, ['Thor', 'Hulk', 86, [55, 77, 64, 'Avengers']]]]
nested_list[3][1]
[487, 88, 76, ['Thor', 'Hulk', 86, [55, 77, 64, 'Avengers']]]
nested_list[3][1][3]
nested_list[3][1][3][-1]
nested_list[3][1][3][-1][-1]
'Avengers'
# Bitwise operations
10 & 4
bin(10)
'0b1010'
1010
bin(4)
'0b100'
100
1 0 1 0
0 1 0 0
and
----------------
0000
0b0000
# | ----> bitwise or
44 | 56
60
bin(44)
'0b101100'
bin(56)
'0b111000'
1 0 1 1 0 0
1 1 1 0 0 0
or
---------------
1 1 1 1 0 0
0b111100
60
44 ^ 56
20
1 0 1 1 0 0
1 1 1 0 0 0
XOR
---------------
0 1 0 1 0 0
0b010100
20
(1+2j) / (2+2j)
(0.75+0.25j)