Skip to main content

Python variables and datatypes

Python variable declaration is  allocate the memory for storing the values. Python datatypes are Numbers, Strings, Lists, Tuples and Dictionary. Python has the good memory management support. In this tutorial we will learn about  What is variable,  Rules for creating variable, Variable Types Variable declaration type. Techtuts provides an example along with concepts. You have to workout the example programs for good and well.

What is variable

Variable provides the memory space for assigning values. Python automatically assign the datatypes to based on the values assigned on the variables.  Each variable can be indentified by a word, which is called  variable name.

Rules for creating variable name

  1. It should contains alphbetic, Numbers and underscore only.
  2. It must not start with numbers.
  3. Spaces are not alowed witin the variable name.

Data types

Python  has different types of data types.  Python has the five primary datatypes. The data type of python are as follows.

  1. Number
  2. String
  3. List
  4. Tuple
  5. Dictionary

Python Numbers

Python number datatype is used for numeric values. Number object is created when you give the numeric value to the variables

Ex:-

vara = 1

varb = 1234

Python number datatype can be used  for the following numerical  types

  • Int
  • Float
  • Long
  • Complex Numbers

Python String

String is represented as Collection of characters placed  within the Quotes( " ) or single quote( ' ). We can use ( + ) for Concatenate and (*) for string repeat print.

Ex:-

varStr='abc'

varStr="abc"

Python List

It is a python compound data types, A list consists the items separated by comma(,) and enclosed within square Brackets ( [] ). We can add different data type of items in the list items. we can assign Int, Float ,string etc as items of the list.

Ex:-

varList = ['abc', 123, 234.90]

Python Tuples

It is another data type like as list. Instead of square Brackets([]), it enclosed with parentheses ( () ). In tuples we cannot be updated. It is known as read only list

Ex:-

varTuple = ( 'abc', 123, 234.90 )

Python Dictionary

Python dictionaries are hash table type. it has the key and value. we can specify any python data type within the dictionary
This datatype is enclosed within the curly phrases ( {} ) and get the value by using  square brackets ( [] ).

Ex:-

varDictionay ={'name':'jhone', 'age':'25', 'dob':'2021-06-13'}

we can use keys() to get all the keys and values()  to get all values of dictionary

Ex:-

varDictionary.keys() # Print all the keys

varDictionary.values() # Print all the values