We’ll be covering the following topics in this tutorial:
What is a variable in python
Variable is a reserved memory storage location that has an associated symbolic name (called “identifier”), which contains some value (can be literal or other data) that can change. You do not need to declare variables before you use them, or declare its type. An identifier is a name used to identify a variable, function, class, module, or other objects. Literal is a notation for constant values of some built-in type. Literal can be string, plain integer, long integer, floating-point number, imaginary number. All variables in Python are an object. So I create a variable age and name and set that equal to anything i want. For example
age = 12
name = ‘kamal’
Here we are creating variables whose name is age and name are identifiers and who value is 12 and kamal are integer and string are literals, respectively. Now we ever in the python program I call one age, I communicate the value has 12. The main thing about the variable is you can always change the value. Remember, variables are not permanent. The variables are changeable.
Python Type of Variable
Python type of variable based on their scope can be classified into two types:
1. Local variables
2. Global variables
The scope of global variables can be accessed throughout the program body by all functions, whereas the local variable’s scope is restricted inside the function where it’s defined.
In the preceding program – a is a local variable whereas b is a global variable, we could access the local variable only inside the function. It’s defined (func() above) and seeking to predict local variables outside its scope(func()) will return an Error. We could call the global variable everywhere in the program, for example, functions (func()) defined in the program.
Local Variables in Python:
The variables that are declared inside the function’s body are known as local variables. Their scope is restricted to the function, i.e, we could access local variables within the function. If we’re attempting to access local variables beyond the function, we’ll find an error.
Example: Local variables in Python
The variables x and y are only going to use within the function sum(), and they do not exist beyond their function. So hoping to use a local variable outside their scope, might through NameError. So clearly below line won’t operate.
Global variables in Python:
The variables that are declared outside of the function are known as global variables. Global variables can be accessed inside or outside of the functions of the module. Let’s see an example of how a global variable is declare in Python.
Example: Global variables in Python
How to declare variable in python
Some rules need to be followed for valid identifier naming:
1. The identifier’s first character must be a letter of the alphabet (uppercase or lowercase) or an underscore(‘_’).
2. The rest of the identifier name can consist of letters (uppercase or lowercase character), underscores(‘_’) or digits (0-9).
3. Identifier names are case-sensitive. For example, myname and myName are not the same. Identifiers can be of unlimited length.
Example of valid and invalid names for variables
Example of valid and invalid names for variables
|A1||yes||Although it containsa number,the nameA1startswithletter|
|speed||yes||Name consisting of letters.|
|Speed90||yes||Name consisting of letters and numbers, but initiated by letters.|
|First_Name||yes||The symbols underscore (_) and allowed and facilitates reading of big names.|
|First Name||no||Variable names can not contain blank spaces.|
|_b||yes||The underscore (_) is accepted in variable names, even at the beginning.|
|1a||no||Variable names can not begin with numbers.|
Version 3 of Python allows the use of accents in variable names. By default, programs are interpreted using a character set called UTF-8, capable of representing almost all the letters of the known alphabets.
In Multiple Assignment you can declare multiple values to multiple variables in one line. The way work is
In some situation you want to assign same value to multiple variable then you can assign it as
p1= p2 = p3 = “Apple”
What is data type in python
Data types are the keywords that tell the compiler what kind of data you are storing into the variable. Every value has a data-type that determines what operations can be performed on the data. Python has several data types, but the most common are numeric, non-numeric, and Boolean (true/false) data would be the basic data types in python. Everything is an Object in Python, data-types are classes and variables are instance (object) of these classes.
It is a dynamically typed language; hence, we don’t need to define the variable’s type while declaring value. A language is dynamically-typed if the type of a variable checked during run-time.
The following has the standard or built-in data types in python.
Python supports two types of numbers: integers and floating point. (Also supports complex numbers).
Number Data Type in Python
We say that a variable is numeric when stores integer or floating-point numbers.
A number Data Type is any representation of data that has a numeric value. Integers and floats are numeric types, which means they hold numbers. Python supports three types of numeric value:
• Integers: It defined as int. Positive or negative whole numbers (with no fractional part)
Full: int (% d)
Assigning an integer value to an integer variable (% d):
myInt = 12 # Printing variable print ("My Whole Number is") # Printing variable with personalized message print ("whole variable is:% d"% myInt)
Where the operator: %d is replaced by the value that myInt variable is stored.
• Floating-point numbers: It described as a float. Any real number with a floating-point representation in which a decimal symbol or scientific notation denotes a fractional component.
Assigning a decimal value to a floating point variable of type (% f):
MyFloatVal = 1.0 #Printing variable print (MyFloatVal) # Printing variable with personalized message print ("Float Value is % f" % MyFloatVal)
In the previous example, as you can see. Where is the mask %f operator is replaced by the variable content that myFloatVal, it has stored.
You can also assign integer content on a variable floating point NOT the reverse happens.
MyFloatVal = 7.5 # Assigning a value to decimal MyFloatVal = float (7) #Making parse the integer value for the type float
• Complex number: It defined as complex. A figure with a real and imaginary part represented as x+yj. x and y are floats, and j is -1(square root of -1 called an imaginary number).
Boolean Data Type in Python
The Boolean data-type in Python programming has two built-in values, True or False capitalized because they are reserved keywords in the python programming language. Booleans can’t hold any value and therefore are the smallest data-type.
Booleans compare two values and return whether those values are alike. Booleans are crucial to a lot of aspects of programming, such as using if statements.
Strings, lists, and tuples are examples of sequence-type(compound data types) in Python, so-called because they behave like a sequence – an ordered collection of similar or different data types. Python has the following standard sequence data types:
• String: Strings literals are a collection of one or more characters written in single, double or triple quotes.
• List: The only built-in mutable sequence type in Python is the list. A list object is an ordered collection of one or more data items, not necessarily of the same type, enclosed in square brackets ([ and ]).
• Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily of the same type, tuples enclosed in parentheses (( and )).
A list containing no elements is called an empty list, and a tuple with no elements is an empty tuple.
How to declare data type in python
Python is completely object-oriented, and not a strongly typed language; In Python, you don’t require to declare variable data-type, unlike C language explicitly. All variable in Python is an object. The developer can directly write a statement, X = 10 without declaring variable data-type. Based on the value of the variable, Python automatically determined the X variable as an integer. However, Python doesn’t support character type; all characters are strings in Python.
How to check data type in python
In this article, we learn how to find the type of variable in Python with examples. You have to use the type() function of Python to get the type of any variable. You have to use print statement in addition to type function to get the type of any variable.
In Python, There are many types of variables available, Such as Numbers, List, String, Tuple, and Dictionary. These variables created in various ways. Should you choose to check any variable components, you might find it challenging to have the type at the very first appearance.
However, Python comes with a simple type() function to make it easier for developers. You can quickly get the type of any variable using the simple method given here using type().
VarType = [‘Canada’, 001, 11.3, ‘Code’];
The above mentioned example is revealing that the specified variable is a Python list variable. But, it is possible to discover a number of different variables of Python too. The variable could be string, tuple, dictionary.