Python Tutorial - Variables



We covered some basic information about python in the previous blog topic. In this blog, we are going to cover variables in python. Variables are defined as the reserved memory location to store data.


  •  Lists
  • Sets
  • Tuples
  • Dictionary

Lists:

Lists will contain values in an ordered way. The values should be given inside the '[ ]' bracket. 
We can add many values in a single variable in lists. We should use ' ' for alphabets.

Syntax:

       x = ['Gaming','Bug']
       y = [1,2,3]
       
       print(x)
       print(y)

output:
       ['gaming','Bug']
       [21]

  • To add value in the set: x.insert(index,value)       

Example:    
      x = ['Gaming','Bug']
      x.insert(2,'Tech')
      print(x)

Output:
      ['Gaming', 'Bug', 'Tech']

  • To sort the list: x.sort()
  • To remove a specific value from the list: x.remove(index,value)
  • To reverse the values in the list: x.reverse()
  • To delete the entire list: Del x

Tuples:

Tuples are similar to lists. The main differences are that tuple uses '( )' and tuples are immutable, which means it can't be updated like lists.

Syntax:

       x=('Bug'.'Tech')
      
       print (x[0])

Output: 
       
      Bug

The '0' in the print statement denotes the index. The index will start at 0,1,2 and continues. 

Sets:

Set is a collection of unordered values. This values will be given inside { }.

Syntax: 
    
x={'a',2,'b',3}
print(x)

Output:

{'a,' 2, b, 3}

  • To remove an element from the set: x.discard(value)
  • To remove the last element: x.pop()
  • To clear the set: x.clear()
  • These are some of the examples for changing the elements.

Dictionary:

A Dictionary is a collection of elements. It has keys and values. It uses { } brackets.

Syntax:


x={
    "name": "Bug",
    "age": 21
     }

print(x)     

Output:

{'name': 'Bug', 'age': 21}

  • To change values: 
x={
    "name": "Bug",
    "age": 21
     }
x["name"]="Gaming Bug"
print(x)

  • To remove:
x={
    "name": "Bug",
    "age": 21
     }
x.pop=("key to be removed")
print(x)

  • To delete the complete dictionary:
x={
    "name": "Bug",
    "age": 21
     }
del x
print(x) // This will show the error message in output, because we deleted the dictionary.


This blog will give you a basic understanding of the variables in python. Stay tuned for further tutorials. Subscribe to get notifications.

                                             Click here to move to next topic
Contact:

    

Comments

Post a Comment

Popular Posts