Variables in Python
Variables in Python:
Python is case sensitive which means c is considered different from C , J 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 −
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
- In the next line print out the value of variable
price . Make sure you use the
price = 10
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 # 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!Hello World!
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
printing = 10
print_variable = 10
int_var = 121