Python VariablesVariable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value. In Python, we don't need to specify the type of variable because Python is a infer language and smart enough to get variable type. Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore. It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two different variables. Identifier NamingVariables are the example of identifiers. An Identifier is used to identify the literals used in the program. The rules to name an identifier are given below.
Declaring Variable and Assigning ValuesPython does not bind us to declare a variable before using it in the application. It allows us to create a variable at the required time. We don't need to declare explicitly variable in Python. When we assign any value to the variable, that variable is declared automatically. The equal (=) operator is used to assign value to a variable. Object ReferencesIt is necessary to understand how the Python interpreter works when we declare a variable. The process of treating variables is somewhat different from many other programming languages. Python is the highly object-oriented programming language; that's why every data item belongs to a specific type of class. Consider the following example. Output: John The Python object creates an integer object and displays it to the console. In the above print statement, we have created a string object. Let's check the type of it using the Python built-in type() function. Output: In Python, variables are a symbolic name that is a reference or pointer to an object. The variables are used to denote objects by that name. Let's understand the following example ![]() In the above image, the variable a refers to an integer object. Suppose we assign the integer value 50 to a new variable b. a = 50 b = a ![]() The variable b refers to the same object that a points to because Python does not create another object. Let's assign the new value to b. Now both variables will refer to the different objects. a = 50 b =100 ![]() Python manages memory efficiently if we assign the same variable to two different values. Object IdentityIn Python, every created object identifies uniquely in Python. Python provides the guaranteed that no two objects will have the same identifier. The built-in id() function, is used to identify the object identifier. Consider the following example. Output: 140734982691168 140734982691168 2822056960944 We assigned the b = a, a and b both point to the same object. When we checked by the id() function it returned the same number. We reassign a to 500; then it referred to the new object identifier. Variable NamesWe have already discussed how to declare the valid variable. Variable names can be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_). Consider the following example of valid variables names. Output: Devansh 20 80.5 Consider the following valid variables name. Output: A B C D E D E F G F I In the above example, we have declared a few valid variable names such as name, _name_ , etc. But it is not recommended because when we try to read code, it may create confusion. The variable name should be descriptive to make code more readable. The multi-word keywords can be created by the following method.
Multiple AssignmentPython allows us to assign a value to multiple variables in a single statement, which is also known as multiple assignments. We can apply multiple assignments in two ways, either by assigning a single value to multiple variables or assigning multiple values to multiple variables. Consider the following example. 1. Assigning single value to multiple variables Eg: Output: 50 50 50 2. Assigning multiple values to multiple variables: Eg: Output: 5 10 15 The values will be assigned in the order in which variables appear. Python Variable TypesThere are two types of variables in Python - Local variable and Global variable. Let's understand the following variables. Local VariableLocal variables are the variables that declared inside the function and have scope within the function. Let's understand the following example. Example - Output: The sum is: 50 Explanation: In the above code, we declared a function named add() and assigned a few variables within the function. These variables will be referred to as the local variables which have scope only inside the function. If we try to use them outside the function, we get a following error. Output: The sum is: 50 print(a) NameError: name 'a' is not defined We tried to use local variable outside their scope; it threw the NameError. Global VariablesGlobal variables can be used throughout the program, and its scope is in the entire program. We can use global variables inside or outside the function. A variable declared outside the function is the global variable by default. Python provides the global keyword to use global variable inside the function. If we don't use the global keyword, the function treats it as a local variable. Let's understand the following example. Example - Output: 101 Welcome To Javatpoint Welcome To Javatpoint Explanation: In the above code, we declare a global variable x and assign a value to it. Next, we defined a function and accessed the declared variable using the global keyword inside the function. Now we can modify its value. Then, we assigned a new string value to the variable x. Now, we called the function and proceeded to print x. It printed the as newly assigned value of x. Delete a variableWe can delete the variable using the del keyword. The syntax is given below. Syntax - In the following example, we create a variable x and assign value to it. We deleted variable x, and print it, we get the error "variable x is not defined". The variable x will no longer use in future. Example - Output: 6 Traceback (most recent call last): File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/multiprocessing.py", line 389, in Maximum Possible Value of an Integer in PythonUnlike the other programming languages, Python doesn't have long int or float data types. It treats all integer values as an int data type. Here, the question arises. What is the maximum possible value can hold by the variable in Python? Consider the following example. Example - Output:
As we can see in the above example, we assigned a large integer value to variable x and checked its type. It printed class Python doesn't have any special data type to store larger numbers. Print Single and Multiple Variables in PythonWe can print multiple variables within the single print statement. Below are the example of single and multiple printing values. Example - 1 (Printing Single Variable) Output: 5 5 Example - 2 (Printing Multiple Variables) Output: 5 6 1 2 3 4 5 6 7 8 Basic Fundamentals:This section contains the fundamentals of Python, such as: i)Tokens and their types. ii) Comments a)Tokens:
There are following tokens in Python:
We will discuss above the tokens in detail next tutorials. Next TopicPython Data Types
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com