Python Variables and Datatypes
A variable is a name given to any storage area or memory location in a program.
In simple words, we can say that a variable is a container that contains some information, and whenever we need that information, we use the name of that container to access it. Let's create a variable:
Here a and b are variables, and we can use a to access 34 and b to access 23.2. We can also overwrite the values in a and b
Data Types in Python:
Primarily there are the following data types in Python.
1. Integers (
2. Floating point numbers (
3. Strings (
4. Booleans (
5. None: None is literal to describe 'Nothing' in Python
Rules for defining a variable in Python:
A variable name can contain alphabets, digits, and underscores (_). For E.g. : demo_xyz = ‘It’s a string variable’
A variable name can only start with an alphabet and underscore.
It can't start with a digit. For example, 5harry is illegal and not allowed.
No white space is allowed to be used inside a variable name.
Also, reserved keywords are not recommended to be used as variable names.
Examples of few valid variable names are harry, _demo, de_mo, etc.
Python is a fantastic language that automatically identifies the type of data for us. It means we need to put some data in a variable, and Python automatically understands the kind of data a variable is holding. Cool, isn't it?
Have a look at the code below:
type() Function in Python:type() function is a function that allows a user to find data type of any variable. It returns the data type of any data contained in the variable passed to it.
Have a look at the code below, which depicts the use of type function:
Note – We can't do numbers with strings arithmetic operations, i.e., we can't add a string to any number. Have a look at the example below:
Note – We can add (concatenate) two or more strings, and the strings will be concatenated to return another string. Here is the example showing that: