Variables in Python

Variables in Python:

Everything in Python is case sensitive which means c is considered different from C  ,   is considered different from j , and so on. Following is an example of a variable named character  and assign the value "Ninja Panda"  to that variable.

character = "Ninja Panda"


Standard Data Types:

The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.

Python has five standard data types −

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Number data types store numeric values. Number objects are created when you assign a value to them. For example −

  • Create a variable named price  and assign it a value of 10 .
  • In the next line print out the value of variable price . Make sure you use the print  function.
price = 10


Python Strings:

Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For example −


str = 'Hello World!'

print str          # Prints complete string
print str[0]       # Prints first character of the string
print str[2:5]     # Prints characters starting from 3rd to 5th
print str[2:]      # Prints string starting from 3rd character
print str * 2      # Prints string two times
print str + "TEST" # Prints concatenated string

This will produce the following result −

Hello World!
llo World!
Hello World!Hello World!
Hello World!TEST

Do not use Python keywords to name your variables because there might be name clashes between your variable name and the reserved keyword. Following are some example.

Avoid the following:

print = 10 

int = 121 

Good examples:

printing = 10 

print_variable = 10 

int_var = 121 

Python 12


Explore Tutu'rself