Skip to main content

Datatypes in Python...

 Basic datatypes  in python are

  • String
  • Integer
  • Float
  • Complex Numbers 
  • Built-in datastructures 
  1. list
  2. tuple
  3. set
  4. dictionary

  • Built-in methods on String:

stringname.capitalize() - First letter of string is converted in uppercase

stringname.casefold()  - Converts string into lower case

stringname.upper()-Coverts the whole string into uppercase.

stringname.lower()-Converts the whole string into lowercase.

stringname.count()- Prints the number of times a specific value is occurred

stringname.isnumeric() - Returs whether the values in string are numeric or not

stringname.isupper() - Returns whether the string is in uppercase or not i.e True or False

stringname.islower() - Returns whether the string is in lowercase or note i.e True or False

stringname.startswith("suffix") - Tells whether the string starts with particular suffix

stringname.endswith("prefix") - Tells whether the string ends with particular prefix

for more similar methods run print(dir("stringname"))

List

  • Properties of List
List are ordered 
List are mutable
List are dynamic
List elements can be accessed by index
  • Built-in methods of List

 listname.insert(index,element) - Inserts single element into the List at specific index

listname.append(element) - Inserts single element at end of the list.

listname.extend([element1,element2,element3,..,,]) - Inserts more than one elements

listname.remove(element) - removes particular element from list

listname.pop(index) - removes  element with particular index from the list.

listname.count(element) - returns the number of elements with specified values.

listname.reverse() - turns list into reverse order.

for more set methods run print(dir(listname))

Sets

  • Properties of Sets
Sets are unordered.
Duplication of elements is allowed.
Set elements are unique.
Elements cannot be accessed using index.


  • Built-in methods of Sets

setname.add(element) - adds single element into the set

setname.update([element1,element2,element3])- update the number of elements into the list

setname.remove(element) - Removes a particular element.

setname.pop()- pops a element from the set (It will pop any random element as its unordered)

for more set methods run print(dir(setname))


Dictionary

  • Properties of Dictionary

More than one entry per key is not allowed.
Value of dictionary can be of any type.

  • Built-in methods of Dictionary
dictname.keys()-  Returns all the keys from key value pair in dictionary
dictname.values() -Returns all the values from key value pair in dictionary
dictname.pop()-Pops elements with specific key-value pair
dictname.update({Key:value})- Updates the key value pair in dictionary
dictname.items()-Returns list containing tuple for each key value pair 

 for more set methods run print(dir(dictname))

Tuple

  • Properties of Tuples
Indexing is allowed 
Tuples are Ordered
Duplication is allowed
Cannot be altered as immutable



-Rushi Mahapure


Comments

Popular posts from this blog

If Else Statement

Decision making is required when we want to execute a code if a specific condition is satisfied. If else is used for Decision Making Syntax:    if test expression:               Code to be executed if condition in True .   else:               Code to be executed if condition in False. Here code will only be executed if the condition provided for IF statement is True. Else it will Run the code under the Else Statement Sample Example on If else: a = 10 if a == 10:      print("The Number is 10") else:     print("The Number is not 10") if else and elif flow chart: If elif else: elif if is short cut for else if. If the the first if condition is False it goes on to check next elif condition and so on. If all the conditions are false it will enter into else and execute the code inside else. Syntax: if test expression 1:          Code to be exec...

Practice questions on Datatypes and Datastructure

Here are some examples for your practice: Examples on Datatypes. 1.)  j = 100 k = 99 j != k k = j print(k)  2.) c = 4*1**4 print(c) 3.)b = int(22.22+2/2) print(b) 4.)a = "7" b = int(a) print(len('apple')*a) Example on Datastructures 6.) a = 1,2,3,4,5 print(type(a)) 7.)Tuple1= ("Black",[1,2,3],[4,5,6]) print(Tuple[1][1]) 8.)Tuple2= "White",10,"Green" a,b,c=Tuple2 print(a) 9.)Tuple3=(1,2,3,4,5,6,7,8,9) print(Tuple3[2:5],Tuple3[:4],Tuple[3:]) 10.)Tuple4=(10,20,30,40,50) print(Tuple4[-1]) print(Tuple4[-4:-1]) 11.)List1 = [4,3,9,7] List1[1:4] = [2,8,6] print(List1) 12.)List2=['xyz','zyx','yxz'] print(max(List2)) 11.)a,b,*c,d,e=[1,2,3,4,5,6] print(c) 12.)Set1={"Lion","Tiger","Zebra"} Set1.update("Rhino") print(Set1) 13.)Set2={"Lion","Tiger","Zebra"} y={"Rhino"} Set2.update(y) print(Set2) 14.)d = {} print(type(d)) 15.) Exercise on list: You ha...