Tuples:
-Tuple datatype used to store multiple types of data into one variable.
-Tuple allowing duplicates.
-Tuples unchangeable means immutable you can add , delete an elements from tuple
-Tuples are indexed, you can access the elements based on the index
-Tuples are written with function braces
len( ) : return the length of the list function that is number of elements in a list
mytuple = ("C","CPP", "Python" ,"Java","Microsoft.NET","Java")
len(mytuple) result is 6
print(mytuple[0]) returns C
print(mytuple[-1]) returns Java
print(mytuple[0:3]) returns ('C', 'CPP', 'Python')
print(mytuple[:3]) returns ('C', 'CPP', 'Python')
print(mytuple[1:]) returns ('CPP', 'Python', 'Java', 'Microsoft.NET', 'Java')
print(mytuple[:]) returns ('C', 'CPP', 'Python', 'Java', 'Microsoft.NET', 'Java')
print(mytuple[:-3]) returns ('C', 'CPP', 'Python')
print(mytuple[-3:]) returns ('Java', 'Microsoft.NET', 'Java')
print(mytuple[::-3]) returns ('Java', 'Python')
print(mytuple[::3]) returns ('C', 'Java')
Tuple packing/unpacking
tuple1 = ("Python", "2000", "33.4") #assigning values to the tuple
(lang, year, version) = tuple1 #here unpacking the tuple and assigning the values to another tuple
lang = python , year = 2000 , version = 33.4
mytuple = ("280","250","500")
min(): return the minimum element from the tuple
print(min(mytuple)) returns 250
max(): return max element from the list
print(max(mytuple)) returns 500
del: delete element from the list
Other operations in Tuples
mytuple= mytuple + (305,406,710)
print(mytuple) returns ('280', '250', '500', 305, 406, 710)
print(mytuple*2) returns ('280', '250', '500', 305, 406, 710, '280', '250', '500', 305, 406, 710)
print("280" in mytuple) returns True
print("280" not in mytuple) returns false
Dictionaries
-Dictionary is a datatype used to store multiple values in one variable
-Dictionary storing the in the from of Key-Value pair
-Dictionary is mutable you can add , delete and change the elements
-DictiDictionary nary not allowing the duplicate keys
-Form 3.7 onwords dictionaries are ordered
-Dictionary write in curly braces
Example
mydict = {
"lang": "Python",
"version": "3.7",
"year": 2018,
"position":"1"
}
operations:
mydict["lang"]
output : returns the "Python"; extracting a value for a specified key in the dictionary
mydict["version"] = "3.8"
output : returns the "3.8"; modifying the a value for a specified key in the dictionary
mydict["type"] = "opensource"
output : returns "opensource" , adds the new Item to the dictionary
del mydict["type"]
output : delete "type" key-value pair from the dictionary
"version" in mydict
output : returns True if specified key present in dictionary
"version" not in mydict
output : returns False if specified key present in dictionary
Functions/Methods with Dictionary
len() : return no of values from the dictionary .
output: returns 5
key() : return list of keys from the dictionary
output: returns dict_keys(['lang', 'version', 'year', 'position', 'type'])
values() : return list of values from the dictionary
output: returns dict_values(['Python', '3.7', 2018, '1', 'opensource'])
items() :return list of keys-values in from of tuple from the dictionary
output: returns dict_items([('lang', 'Python'), ('version', '3.7'), ('year', 2018), ('position', '1'), ('type', 'opensource')]) list of key-values from the dictionary s a tuple
Conversions between datatype
str(): used to converting to a string
int():used to converting to an integer
float():used to converting to a float
list(): used to converting to a list
tuple() : used to converting to a tuple
set() :used to converting to a set
bin() : used to converting to a binary representation
hex() : used to converting to a hexadecimal representation
Functions
def myfunction(x, y): #defining function with two parameters
sum = x + y
return sum #stop the program execution and returning result
myfunction(2,3) #calling a function and passing two positional arguments
myfunction(x=2,y=3) #calling a function and passing two keyword arguments
myfunction(2,y=3) #calling a function and passing two mixed arguments
myfunction(2,*args) #args is a tuple
File operations in Python:
FileModes:
"r" : Read the data from the file, If file not found in specified path throwing an error
"a": append data to existing file, If file not found in specified path create the file
"w": write data to the file, If file not found in specified path create the file
"w+": both writing and reading at the same time
"x": Create the specified file, if file exist in specified path throwing an error
"b": Binary mode data purpose
"t" : Default text mode purpose
File operations
open() : used to open the file
read(): return whole file content in the form of string
seek(): return cursor to the specified position in the file.
readline(): returns the file content one line at a time, each time you use the method
readlines() :returns the file content in the form of list
writelines(): this method takes a sequence of strings as an argument and writes those strings to the file
tell( ) : returning the cursor position in file
close() :Used to close the file
best post.
ReplyDeleteonline training in python
online training on python