Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

readme.md

variables in python

common variables datatypes are

  • integer => 12
  • float => 12.00
  • boolean => True/1 , False/0
  • string => 'vaibhav'
  • complex => 10+j

String


string in python are immutable means we can't change the value once we assign in a variable.

name = "vaibhav jain"
# we assign value in name variable if we want to change the value of any index than python won't allow us to do that for example
name[0:3] = abc  # this code give error

string in python represented as object means we can use indexing in string.

name = "vaibhav"
name[0] # -> v
name[0:3]  # -> vai
name[:4]  # -> vaib
name[-1] # -> v
len(name) # 7

Note:- Len() is use words of the string.


List


List in Python is special datatype which helps in holding multiple data in single variable.

nums = [1,2,34,5,6]

List values are accessible using indexing similar as string.

nums = [1,2,34,5,6]
print(nums[-1]) # 6
print(nums[-3]) # 34
print(nums[2:]) # 5,6

List is mutable datatype therefor we can change the value of any index of the string.

nums = [1,2,34,5,6]
nums[0] = 5
print(nums) # [5,2,34,5,6]

List contains can take any type of data. it is not required to put similar datatypes.

nums = ["vaibhav" , 8, "jain", True, 19.89]

Len() is used for count length of the list

nums = [1,2,3,45,6]
print(len(nums)) # 5

Only Addition is allowed between 2 Lists.

nums1 = [1,2,3]
nums2 = [1,2,3]
print(num1+num2) # [1,2,3,1,2,3]

There are n number of function we can perform with list which is predefined in python some of them are insert , append, remove, clear etc.

  1. append :- take on parameter as a value and append at the end of list
nums1 = [1,2,3,45,5,45]
nums1.append(22)
print(nums1) #[1, 2, 3, 45, 5, 45, 22]
  1. clear :- Clear all elements of the list
nums1 = [1,2,3,45,5,45]
nums1.clear()
print(nums1) #[]
  1. copy :- return copy of the list.
nums1 = [1,2,3,45,5,45]
print(nums1.copy()) #[1, 2, 3, 45, 5, 45]
  1. count :- take one value as param and return count of its presence.
nums1 = [1,2,3,45,5,45]
print(nums1.count(45)) #2
  1. index :- Index takes value as argument and return its index
nums1 = [1,2,3,45,5,45]
print(index(45)) # 3 always return 1st presence.
  1. pop :- pop takes an index as argument and remove it from list.
num1 = [1,2,3,45,6,7]
num1.pop(2)
print(num1) # [1,2,45,6,7]
  1. remove :- remove takes value as argument and remove it from list.
num1 = [1,2,34,56,5]
num1.remove(1)
print(num1) # [2,34,56,5]
  1. sort :- Sort list in assending/descending order.
nums1.sort()
print(nums1)

# we can also extend sort in many ways
#  sort accept 2 params sort(reverse=True, key=myFunc)

Tuples


Tuples is also a special datatype in python which is quite similar like list but it is written in paranthesis ()

tup = (1,2,34,5)

Tuples are immutable means we can't change the value of the tuple.

Tuple supports only 2 functions

  1. index :- Index takes value as argument and return its index
nums1 = (1,2,3,45,5,45)
print(index(45)) # 3 always return 1st presence.
  1. count :- take one value as param and return count of its presence.
nums1 = (1,2,3,45,5,45)
print(nums1.count(45)) #2

ADVANTAGE OF TUPLE :~ Itration of tuple is faster than list.


Sets


Sets are another special datatype in python use for store multiple values. sets are initialise in {}.

s = {1,24,56,78}

Sets don't contain any sequence so they are not accessible by index or they won't contain index.

sets are work on hash so they process faster than list.

sets doesn't contain duplicate values.