• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » Python Type of Variable
Next →
← Prev

Python Type of Variable

By Dinesh Thakur

We’ll be covering the following topics in this tutorial:

  • What is a variable in python
  • Python Type of Variable
  • How to declare variable in python
  • What is data type in python
  • How to declare data type in python
  • How to check data type in python

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.

def func(): a = "Learn Python " 
   print(a) 
   print(b) 
   b = "By ecomputernotes" 
   print(b) 
   func() 
     print(a) 

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

def sum(x,y): 
  sum = x + y 
  return sum 
  print(sum(10, 10)) 
  Output: 20 

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

x = 5 
def func(): 
  global x 
  print(x) 
  x=10 
   func() 
   print(x)  
Output : 5 10 

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

Name

Valid

Comment

A1yesAlthough it containsa number,the nameA1startswithletter
speedyesName consisting of letters.
Speed90yesName consisting of letters and numbers, but initiated by letters.
First_NameyesThe symbols underscore (_) and allowed and facilitates reading of big names.
First NamenoVariable names can not contain blank spaces.
_byesThe underscore (_) is accepted in variable names, even at the beginning.
1anoVariable 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.

Multiple Assignment

In Multiple Assignment you can declare multiple values to multiple variables in one line. The way work is

p1,p2,p3=”aman”,”kamal”,”sachin”

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.

Sequence Type

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’];
print(type(VarType));

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.

 

You’ll also like:

  1. What do you means by C++ Tokens? Explain Variable,Data Type,Constants, Identifiers and Keyword
  2. What is Python? | Introduction to Python Programming
  3. Python Features | Main Features of Python Programming Language
  4. Python Operators and Operands – Types of Operators in Python
  5. Final Variable (OR Constant Variable) in Java with Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.