Python Variables, Data Types & Keywords
Now we are getting used to Python and its way, we think it is time to sharpen Python Variables, Datatypes, and Keywords in our brains. First, let's start with Python variables. Here we go...
Python Variables:
A Python Variable is a reserved memory location to store values. In other words, a variable in a Python program gives data to the computer for processing. Python variable is also known as an identifier and used to hold value.
Now, while declaring variables, we need to keep certain things in mind. Before we understand that things in mind, look at this, How we can declare variables in Python Programming Language.
Rules of declaring Variables in Python:
- A variable name start with a letter or an underscore character.
- A variable name cannot start with numbers.
- A variable name can only contain alpha-numeric character and underscore(A-z, 0-9, _)
- Variable names are case-sensitive (techie, Techie, TECHIE are different variables)
Remember that variable's name are case-sensitive.
#Legal Variable Name
var="Techiemania"
_var="Techiemania"
VAR="Techiemania"
var1="Techiemania"
#Illegal Variable Name
2var="techiemania"
my var="Techiemania"
my - var="Techiemania"
Assign the value to multiple Variables
Code:
x,y = "Techie", "Mania"
print(x)
print(y)
Output:
Techie
Mania
And you can assign the same value to multiple variables in one line:
Code:
x = y = "Techie Mania"
print(x)
print(y)
Output:
Techie Mania
Techie Mania
Output Variable:
The Python print statement is often used to output Variables
To combine both text and a variable, Python uses the + character
Code:
x = "Mania"
print("Techie" + x)
Output:
Techie Mania
You can also add the + character to add a variable to another variable.
Code:
x = "Techie"
y = "Mania"
z = x+y
print(z)
Output:
Techie Mania
For numbers, the + character works as mathematical operator
Code:
x = 5
y = 10
print(x+y)
Output:
15
If you try to combine a string and number, Python will give you an error
Code:
x = 5
y = "Techie"
print(x + y)
Output:
Type error: unsupported operand type(s) for +: 'int' and 'string'
Global Variable:
Variables that are created outside of a function are known as Global Variables.
Global Variables can be used by everyone, both inside of a function and outside.
Create a variable outside a function, and use it in inside the function
Code:
x = "Mania"
def myfunc( ):
print("Techie" + x)
myfunc( )
Output"
Techie Mania
If you create a Variable with the same name inside a function, this variable will be local, and can only be used inside the function. The Global Variable with the same name will remain as it was, global and with the original value.
Create a Variable inside a function, with the same name as the global variable.
Code:
x = "awesome"
def myfunc( ):
x = "fantastic"
Print("TechieMania is" + x)
myfunc( )
print("TechieMania is" + x)
Output:
TechieMania is fantastic
ThecieMania is awesome
Python Data types:
In programming, a data type is an important concept.
Variables can store data of different types, and different types can do different things.
Hence, we do not need to define the type of the variable while declaring it.
a = 10
The variable holds integer value ten and we did not define its type. Python interpreters will automatically interpreter variables as an integer type.
Consider the following example to define the values of different data types and checking their type.
Code:
a = 10
b = "TechieMania"
c = 10.5
print(type(a))
print(type(b))
print(type(c))
Output:
<type, 'int'>
<type, 'string'>
<type, 'float'>
Standard data type:
A variable can hold different types of values. For example, a person's name must be stored as string whereas its id must be stored as an integer.
Python provides various standard data types that define the storage method on each of them. The data types defined in python are given below:
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Built-in Data types:
Python has the following data types built-in by default, in these categories:
Text Type: str
NumericTypes: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Type: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Getting the Data Type:
You can get the data type of any object by using the type( ) function:
Code:
x = 10
print(type(x))
Output:
<class, 'int'>
Python Keywords:
Python keywords are special reserved words that convey a special meaning to the compiler. Each keyword has a special meaning and a specific operation. These keywords can't be used as a variable.
Following is the list of Python keywords:
Hope this article found you interesting and helpful.
Thank You.
Output Variable:
The Python print statement is often used to output Variables
To combine both text and a variable, Python uses the + character
Code:
x = "Mania"
print("Techie" + x)
Output:
Techie Mania
You can also add the + character to add a variable to another variable.
Code:
x = "Techie"
y = "Mania"
z = x+y
print(z)
Output:
Techie Mania
For numbers, the + character works as mathematical operator
Code:
x = 5
y = 10
print(x+y)
Output:
15
If you try to combine a string and number, Python will give you an error
Code:
x = 5
y = "Techie"
print(x + y)
Output:
Type error: unsupported operand type(s) for +: 'int' and 'string'
Global Variable:
Variables that are created outside of a function are known as Global Variables.
Global Variables can be used by everyone, both inside of a function and outside.
Create a variable outside a function, and use it in inside the function
Code:
x = "Mania"
def myfunc( ):
print("Techie" + x)
myfunc( )
Output"
Techie Mania
If you create a Variable with the same name inside a function, this variable will be local, and can only be used inside the function. The Global Variable with the same name will remain as it was, global and with the original value.
Create a Variable inside a function, with the same name as the global variable.
Code:
x = "awesome"
def myfunc( ):
x = "fantastic"
Print("TechieMania is" + x)
myfunc( )
print("TechieMania is" + x)
Output:
TechieMania is fantastic
ThecieMania is awesome
Python Data types:
In programming, a data type is an important concept.
Variables can store data of different types, and different types can do different things.
Hence, we do not need to define the type of the variable while declaring it.
a = 10
The variable holds integer value ten and we did not define its type. Python interpreters will automatically interpreter variables as an integer type.
Consider the following example to define the values of different data types and checking their type.
Code:
a = 10
b = "TechieMania"
c = 10.5
print(type(a))
print(type(b))
print(type(c))
Output:
<type, 'int'>
<type, 'string'>
<type, 'float'>
Standard data type:
A variable can hold different types of values. For example, a person's name must be stored as string whereas its id must be stored as an integer.
Python provides various standard data types that define the storage method on each of them. The data types defined in python are given below:
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Built-in Data types:
Python has the following data types built-in by default, in these categories:
Text Type: str
NumericTypes: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Type: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Getting the Data Type:
You can get the data type of any object by using the type( ) function:
Code:
x = 10
print(type(x))
Output:
<class, 'int'>
Python Keywords:
Python keywords are special reserved words that convey a special meaning to the compiler. Each keyword has a special meaning and a specific operation. These keywords can't be used as a variable.
Following is the list of Python keywords:
true
|
false
|
none
|
and
|
as
|
asset
|
def
|
class
|
continue
|
break
|
else
|
finally
|
Elif
|
del
|
except
|
global
|
for
|
if
|
from
|
import
|
raise
|
try
|
or
|
return
|
pass
|
nonlocal
|
in
|
not
|
is
|
lambda
|
Hope this article found you interesting and helpful.
Thank You.
~ Jinal Menpara
Nice one 👍 keep it up.
ReplyDelete☺️🤗
DeleteGreat work 👌
ReplyDeletevery good
ReplyDeleteIt impresive
ReplyDelete