Roshan Book

My Tech Notebook

Tag Archives: tuple

Python tutorial 6 lists and its methods

# List

# List Methods
# For the list
cast = [“Cleese”, “Palin”, “Jones”, “Idle”]
print (cast)

# finding length
print (len(cast))

# Print specific position value
print (cast[3])

# Append – adds to last of the list
cast.append(“Gilliam”)
print (cast)

# Pop- remove last item

cast.pop()

# Extend – adds multiple items at the end
cast.extend([“Gilliam”, “Chapman”])

# Remove – remove specific data

cast.remove(“Chapman”)

# Insert data to specific postion
cast.insert (0,”Chapman”)

 

Iterate (moving along each item) along list

fav_movies =[“manish”, “roshan”, “is”]

for each_item in fav_movies:

print(each_item)

# List inside list

movies=[“Tere Naam”, Hindi[“Marigold”, “Indian”]]

accessing value – print (movies[1][0])- output Marigold

Advertisements

Python tutorial 5 Variables Objects and Values

Everything in python is an object

All object has an ID, Type and Value

Mutable object change values – Lists, dictionaries

Immutable object does not change values – numbers, strings, tuples

# Checking ID, Value and Type of a variable

>>> x =42
>>> id(x)
506091328
>>> type(x)
<class ‘int’>

# Numbers

int and float

num=42//9 , gives integer answer and not rounded up – 4

num = round(42/9, 2) – round to 2 places of decimal- 5

num= 42%9 – reminder 6

# Strings

print (r“this is n string”) – adding r removes any backslash effect and print n as it is

Printing variable

n=42
s=”This is a {} string”.format(n)
print(s)

format is a method in string object

‘’’ ‘’’ – triple quotes helps to print multiple lines

#Tuple

x=(1,2,3) – immutable . you cannot append things

 

#List

x= [1,2,3] – mutable. You can add, change, delete in Lists

# Dictionary

x = {‘one’:1, ‘two’:2}

x==y compare their values

x is y -check if id is same

%d bloggers like this: