Thanks to visit codestin.com
Credit goes to www.tutorialsteacher.com

Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - Get Started
  • What is Python?
  • Where to use Python?
  • Python Version History
  • Install Python
  • Python - Shell/REPL
  • Python IDLE
  • Python Editors
  • Python Syntax
  • Python Keywords
  • Python Variables
  • Python Data Types
  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Python Operators
  • Python Conditions - if, elif
  • Python While Loop
  • Python For Loop
  • User Defined Functions
  • Lambda Functions
  • Variable Scope
  • Python Modules
  • Module Attributes
  • Python Packages
  • Python PIP
  • __main__, __name__
  • Python Built-in Modules
  • OS Module
  • Sys Module
  • Math Module
  • Statistics Module
  • Collections Module
  • Random Module
  • Python Generator Function
  • Python List Comprehension
  • Python Recursion
  • Python Built-in Error Types
  • Python Exception Handling
  • Python Assert Statement
  • Define Class in Python
  • Inheritance in Python
  • Python Access Modifiers
  • Python Decorators
  • @property Decorator
  • @classmethod Decorator
  • @staticmethod Decorator
  • Python Dunder Methods
  • CRUD Operations in Python
  • Python Read, Write Files
  • Regex in Python
  • Create GUI using Tkinter
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Python Variables

In Python, a variable is a container that stores a value. In other words, variable is the name given to a value, so that it becomes easy to refer a value later on.

Unlike C# or Java, it's not necessary to explicitly define a variable in Python before using it. Just assign a value to a variable using the = operator e.g. variable_name = value. That's it.

The following creates a variable with the integer value.

Example: Declare a Variable in Python
num = 10
Try it

In the above example, we declared a variable named num and assigned an integer value 10 to it. Use the built-in print() function to display the value of a variable on the console or IDLE or REPL.

In the same way, the following declares variables with different types of values.

Example: Variables
num = 10 #integer variable
amount = 78.50 #float variable
greet='Hello World' #string variable
isActive = True #boolean variable
Try it

Multiple Variables Assignment

You can declare multiple variables and assign values to each variable in a single statement, as shown below.

Example: Create Multiple Variables
x, y, z = 10, 20, 30
print(x, y, z)  #10 20 30
Try it

In the above example, the first int value 10 will be assigned to the first variable x, the second value to the second variable y, and the third value to the third variable z. Assignment of values to variables must be in the same order in they declared.

You can also declare different types of values to variables in a single statement separated by a comma, as shown below.

Example: Create Multiple Variables
x, y, z = 10, 'Hello', True
print(x, y, z)  #10 Hello True
Try it

Above, the variable x stores 10, y stores a string 'Hello', and z stores a boolean value True. The type of variables are based on the types of assigned value.

Assign a value to each individual variable separated by a comma will throw a syntax error, as shown below.

Example: Syntax Error
x = 10, y = 'Hello', z = True
Try it

Objects

Variables in Python are objects. A variable is an object of a class based on the value it stores. Use the type() function to get the class name (type) of a variable.

Example: Variable Type
num = 10 
type(num)  #<class 'int'>

amount = 78.50 
type(amount)  #<class 'float'>

greet='Hello World' 
type(greet)   #<class 'str'>

isActive = True 
type(isActive)  #<class 'bool'>
Try it

In the above example, num is an object of the int class that contains integre value 10. In the same way, amount is an object of the float class, greet is an object of the str class,isActive is an object of the bool class.

Unlike other programming languages like C# or Java, Python is a dynamically-typed language, which means you don't need to declare a type of a variable. The type will be assigned dynamically based on the assigned value.

Example: Variable Type
x = 100
print(type(x))  #<class 'int'>

x = 'Hello World!'
print(type(x))  #<class 'str'>
Try it

The + operator sums up two int variables, whereas it concatenates two string type variables.

Example: + Operator
x = 100
print(x + 10)   #110

x = 'Hello'
print(x + ' Python')  #Hello Python
Try it

Object's Identity

Each object in Python has an id. It is the object's address in memory represented by an integer value. The id() function returns the id of the specified object where it is stored, as shown below.

Example: Object Id
x = 100
id(x)

greet='Hello'
id(greet)
Try it

Variables with the same value will have the same id.

Example: Same Objects Ids
x = 100
y = x;
z = 100

print(id(x), id(y), id(z))
Try it

Thus, Python optimize memory usage by not creating separate objects if they point to same value.

Naming Conventions

Any suitable identifier can be used as a name of a variable, based on the following rules:

  1. The name of the variable should start with either an alphabet letter (lower or upper case) or an underscore (_), but it cannot start with a digit.
  2. More than one alpha-numeric characters or underscores may follow.
  3. The variable name can consist of alphabet letter(s), number(s) and underscore(s) only. For example, myVar, MyVar, _myVar, MyVar123 are valid variable names, but m*var, my-var, 1myVar are invalid variable names.
  4. Variable names in Python are case sensitive. So, NAME, name, nAME, and nAmE are treated as different variable names.
  5. Variable names cannot be a reserved keywords in Python.
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.