Sunday, May 10, 2020

sequences in python

Storing values in order and again accessing those values using indexes.

In an order arrange the elements and access that through indexes

Three types of sequences:

1. Lists
2. Tuples
3. Strings

Strings : collection/group of characters.

How to create a string ?

Strings can be used using single quotes and double quotes.

reason may be there is no datatype called character.

Python is a case-sensitive language.

How to update a value of a string ? ( background operation )

intially  => s  = "kaushik"

( what happeneds in the background, In your memory a block is created and the value is stored in that block).

this will create a memory location address, this address is assigned to s

Initially,
  s   ------>    0xc123

 but now, the  s= "surya"

the string is created and new memory block is created,

s  ------->  1xzn4

This kind of datatypes are called immutable 

immutable means  ( we cannot changes the values ).


How to clear the contents of a string ?

By simply assigning empty string 

s = "";

How to remove the string ?

if you dont use that string, automatically the reference account is equal to zero.

or

if you want to remove  it manually.

del s;

How to access the content of a string ?

s = "my name is kaushik"

s[0] ------> 1st character

s[0:5] ------> this is the range  ( return those character ).

[ it returns the substring b/w 0 index to 4 ].

s[5:]  ------>    returns all characters starting from index 5.

s[:10]  ------> returns all characters starting from index 0 to 9


String indexing 

1. Positive indexing      (  0 1 2 3 )
( starts from starting character )

2. Negative indexing    (  -4  -3  -2  -1  )
(starts from right most ending ).


How to find out a length of a string ?


S = "Hello How are you"

length   -------->  len(s)

len is a function

max(s)   -------> according to the  ASCII key numbers if you take.

( which is the bigger number from  "s" is  y )

min(s)   --------->   ' '  ( space has minimum value ).


Built-in functions 



string.capitalize    ---->   it will help you to convert the first letter to capital letter.



s='abcd'




List  ( is one of the sequence ).


represents using  []  

[  it stores set of objects  ]

s = [ 1, 2, 3.5 , "hello", "kaushik" ]

( it can store different kinds of objects ).

[ but string only stores characters and is immutable ].

but  in list, you can add or remove the objects at anytime.

add , remove 

create a list,

s = [ 1,2,3,4,5, "TWG" ]

list lo one more sublist 

How to retrieve the elements from the list ?

by using index 

s[0]  --------------->    index 

s[0:3]   ------------->   range index 


How to update the elements from the list ?

s[1] = 10 




Remove an element from the list 




How to remove the list ?



Operators in list 




Example :



Example01:



Membership operator :

in
not in 





In a list, whether the element exists or not.

it will help you to check.


Concatenation operator 


two lists, how you can concatenate ( by using  (+) operator ).




Repetition operator 

*   ------> star 



Append 





count 




index 




Example 





Insert :




reverse:



Tuples 




Tuples :

tuple is also something similar to list 

()   -----> represents 


also known as Read Only List 

once you create the tuple whatever the elements you cannot update them.


you will try to retrieve similarly like list.



you cannot update/modify the tuples.




How to get the length ?































No comments:

Post a Comment