Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Python » Python Variable and Python Datatype

  • Ramayan : Short Story of ramayana in Hindi | Qualities of Rama
    Ramayan : Short Story of ramayana in Hindi | Qualities of Rama Spiritual
  • Amitabh Bachchan biography in hindi | Family, wife, awards
    Amitabh Bachchan biography in hindi | Family, wife, awards Biography
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
    Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi Biography
  • State the Universal law of gravitation | Law of Gravity, Formula
    State the Universal law of gravitation | Law of Gravity, Formula Science
  • Pythagorean Theorem in Hindi, Definition
    Pythagorean Theorem in Hindi, Definition, Formula, Proof पाइथागोरस थ्योरम क्या है जानिए हिंदी में? Science
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
    Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी Biography
  • Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी
    Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी Biography

Python Variable and Python Datatype

Posted on September 30, 2021October 4, 2021 By GeekCer Education No Comments on Python Variable and Python Datatype
Python Variable and Python Datatype

A python datatype is a core feature of the language, and a python variable is the name of the memory location where the value is stored while the program is running.

We will go through the python variable, variable name, and python datatypes in depth in the article Python Variable and Python Datatype.

Table of Contents

  • Python Variable
    • Variables Naming
    • Define Variables
    • Display Single Variable
    • Display Multiple Variable
    • Multiple assignment in variable
  • Types of Variables in Python
    • 1) Local Variable
    • 2) Global Variable
  • Methods for making variables more readable
    • 1) Camel Case
    • 2) Pascal Case
    • 3) Snake Case
  • Python Datatype
    • 1) int datatype
    • 2) float datatype
    • 3) complex datatype
    • 4) String datatype
    • 5) List datatype
    • 6) Dictionary (dict) datatype
    • 7) Boolean (bool) datatype
    • 8) Set datatype
  • Display the type of variable

Python Variable

Python variable is the name of a memory region where the value is stored while the program is running. Variables in Python don’t need to be declared explicitly. When you assign a value to a variable, the declaration occurs automatically. When assigning values to variables, the equal(=) symbol is utilized.

Variables Naming

  • The variable’s first character must be an alphabet or an underscore (_).
  • Other than the first letter, the remaining characters can be an alphabet (a-z, A-Z), underscore, or number (0-9).
  • There must be no whitespace or special characters in the name (!, @, #, %, ^, &, *).
  • The name must not be identical to any of the language’s keywords.
  • Case matters in names; for example, geekcer and GeekCer are not the same thing.
  • Valid identifiers include x, _var, var20, var20t, _10, and etc.
  • Invalid identifiers include 5n, %t, g c, 5_ etc.

Define Variables

                  x = 10
str = "Geek"
print(x);
print(str);

Display Single Variable

                  a = 5  
print(a)  
print((a)) 

Display Multiple Variable

                  a = 5  
b = 6  
# printing multiple variables  
print(a,b)  
# separate the variables by the comma  
print(1, 2, 3, 4, 5, 6, 7, 8)   

Multiple assignment in variable

Python allows you to assign a single value to many variables at the same time.

                  a = b = c = 20

                  a,b,c=10,20,35

Types of Variables in Python

In Python, there are two types of variables.

  • Local Variable
  • Global Variable

1) Local Variable

                  # Declaring a function  
def add():  
    # Defining local variables. They has scope only within a function  
    a = 20  
    b = 30  
    c = a + b  
    print("The sum is:", c)  
  
# Calling a function  
add()  

2) Global Variable

                  # Declare a variable and initialize it  
x = 101  
  
# Global variable in function  
def mainFunction():  
    # printing a global variable  
    global x  
    print(x)  
    # modifying a global variable  
    x = 'Welcome To GeekCer'  
    print(x)  
  
mainFunction()  
print(x)  

Methods for making variables more readable

There are a variety of approaches that may be used to make variables more readable.

  1. Camel Case (myVariableName)
  2. Pascal Case (MyVariableName)
  3. Snake Case (my_variable_name)

1) Camel Case

Except for the initial word, each word should begin with a capital letter.

                  myVariableName = "GeekCer" 

2) Pascal Case

The first letter of each word should be capitalized.

                  MyVariableName = "GeekCer" 

3) Snake Case

An underscore character should be used to separate each word.

                  my_variable_name = "GeekCer" 

Python Datatype

A programming language’s datatype is a crucial part. Variables can hold data in a variety of formats. When you give a value to a variable in Python, the data type is automatically set.

Python also comes with a number of built-in data types, including dict, list, set and frozenset, and tuple. Unicode strings are stored in the str class, whereas binary data is stored in the bytes and bytearray classes.

A python datatype is a property of data that informs the compiler or interpreter how the programmer wants to use it.

The following are some of Python’s most significant datatypes:

  1. Numeric Types (Int, Float, Complex)
  2. Sequence Types (list, tuple, range )
  3. Mapping Type(dict)
  4. Text Type (str)
  5. Set Types (set, frozenset)
  6. Boolean Types (bool)
  7. Binary Types (bytes, bytearray, memoryview)

1) int datatype

The int datatype is used to hold any integer, such as 10, 20, 2, 50, and so on. In Python, the length of an integer is unrestricted.

                  x = 10
                  

2) float datatype

The float datatype is used to hold floating-point numbers such as 10.02, -20.6, 2.65, and 50.5, among others.

                  x = 10.5
                  

3) complex datatype

The complex datatype is used to hold data that is complex in nature. Ex: x + iy, where x represents actual data and y represents imaginary data.

                  x = 10 + 1j
                  

4) String datatype

Strings, which may be represented in a variety of ways, can also be stored and manipulated in Python. Single quotes (‘…’) or double quotes (‘…’) can be used to enclose them (“…”).

                  str = "GeekCer"
                  
                  str = 'GeekCer'
                  

5) List datatype

The list is a very flexible Python datatype that may be expressed as a list of comma-separated values (items) enclosed in square brackets. Lists in Python are comparable to arrays in programming languages such as C and C++. Lists may contain items of many types, although they generally all belong to the same category.

                  squares = [1, 4, 9, 16, 25]
                  

6) Dictionary (dict) datatype

The data is stored in a dictionary as a key-value pair.

                  x = {"name" : "Geek", "age" : 30}
                  

7) Boolean (bool) datatype

true and false are the built-in values of the Boolean datatype.

                  x = True
                  

8) Set datatype

Sets are used to hold numerous elements in a single, unordered, and unindexed unit. Curly brackets are used to write it. Duplicate items are not allowed in sets.

                  x = {"apple", "banana", "cherry"}
                  

If you are unfamiliar with Python, you may learn more by clicking here.

Display the type of variable

The type() method may be used to determine the type of a variable.

                  x = 30.3
print(type(x)) 
                  

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Python Tags:Data Types, Software Development, Variables

Post navigation

Previous Post: Python Tutorial for Beginners
Next Post: Python List

More Related Articles

Python Tutorial for Beginners Python Tutorial for Beginners Python
Python Tuple and Basic Tuple Operations with Examples Python Tuple and Basic Tuple Operations with Examples Python
Python List Python List Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Python Home
  • Python Variable and Datatype
  • Python List
  • Python Tuple
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved