Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Introduction of Python
  5. Variable and Datatypes in Python

Variable and Datatypes in Python

Variable

A variable in python represents named location that refers to a value and whose value can be used and processed during program run. In python, the Variable is created as soon as you assign any value to it. It doesn’t require any other command unlike any other languages.

There are a certain rules that we have to keep in mind while declaring a variable:

  • The variable name cannot start with a number. It can only start with a character or an underscore.
  • Variables in python are case sensitive.
  • They can only contain alpha-numeric characters and underscores.
  • No special characters are allowed.
a=5       #variable a
b=4       #variable b
print(a+b)

Data types

Since the data to be dealt with are of many types, a programming language must provide ways and facilities to handle all types of data.

  • Numeric
    • Integer
    • Float
    • Complex number
  • Boolean
  • Sequence
    • String
    • Lists
    • Tuples
  • Dictionaries

Numeric- Any representation of data which has numeric value. Python identifies three types of numbers:- Integer, Float, Complex Number.

print(type(5))
print(type(5.6))
print(type(3+5j))

Strings- A collection of one or more characters put in single, double or triple inverted commas are known as string.

print(type("Python"))
output- class 'str'

Lists- A list is a data structure, or it can be considered a container that can be used to store multiple data at once. The list will be ordered and there will be a definite count of it. The elements are indexed according to a sequence and indexing is done with 0 as the first index.

a=['1', '2', '3', '4', '5']
print(type(a))
output- class 'list'

Tuples- An ordered collection of one or more data items, not necessarily of the same type put in parentheses. The contents of a tuple cannot be modified i.e. it is immutable.

a=(1, "python", 3.5, 'true')
print(type(a)) 
output- class 'tuple'

Dictionary- A dictionary is a set of unordered key, value pairs. In a dictionary, the keys must be unique, and they are stored in an unordered manner. To build a dictionary, separate the key-value pairs by a colon(:).

a={'a': 'Python', 'b':'C++', 'c':'Java', 'd':'JavaScript'}
print(type(a))
output- class 'dict'

This is all about datatypes in python.

Was this article helpful to you? Yes No

How can we help?