Social Icons

Thursday, October 20, 2022

Variable In Python

1. Defining a Python variable

A variable is a name of a location in a memory area. Python variable is also known as identifier and used to store a numeric, string or boolean value... In Python, we don't need to specify the type of variable because Python is a dynamically typed language and smart enough to get the type of variable. To declare a variable in python, you don't need to declare its type. A variable in python is declared directly by inserting its name and assigning an initial value. Once the variable is inserted, python automatically detects its type:


n = 25
here we have declared a variable named 'n' and which has a 25 of value. At this time, python detects the type of variable and the class in the category of integers. To display the variable, we use the print() instruction (we will see that it is a predefined function in python)


n = 25
Which display: 25 You can also add an explanatory text


n = 25
print("The value of n is: ", n)
Which displays: The value of n is: 25


name = "Farid"
print("The name is: ", name)
Which displays: The name is: Farid

2. Rules for naming a variable

The Python language allows the user to define their own variables provided they respect a set of rules:
  1. The name of a variable must start with a letter or an underscore: like student1, student2. 1student is not accepted!
  2. All characters except the first character can be an alphabet consisting of lowercase (a-z), uppercase (A-Z), underscores, or numbers (0-9).
  3. The name of a variable must not contain spaces or special characters (!, @, #, %, ^, &, *).
  4. The name of a variable must not be identical to a keyword defined in the language.
  5. The name of a variable is case sensitive: for example, Robert and robert are not the same.
  • Examples of valid variables: bus1 , bus_number_2, _a, b_7, etc.
  • Examples of invalid variables: 3alpha, x%2, self-driving car, etc.

3. Types of python variables

The variable types offered by python are:
  1. integer type or int type, example: 12 , 3 , 77 ...
  2. float type, example: 12.5 , 3.75 , 77.52 ...
  3. string type or str type, example: "robert" , "car" ...
  4. Boolean type: this type takes two values: True and False

How to display the variable type?

To display the type of a variable, we use the function type()


n = 10
Which displays: <class 'int'>


x = 7.55
Which displays: <class 'float'>


name = "robert"
Which displays: <class 'str'>


var = 5 > 7
print("The value of the variable is var = ", var)
print("The type of var is " , type(var))
Which displays: The value of the variable is var = False The type of var is <class 'bool'>

4. Convert or change the variable type

A variable can be easily converted using int(), str() , float() functions.


n = 10
x = float(n)
print("the type of n is: ", type(n))
print("the type of x is: ", type(x))
Which displays: the type of n is: <class 'int'> the type of x is: <class 'float'>


x = 10.0
n = int(x)
print("the type of x is: ", type(x))
print("the type of n is: ", type(n))
Which displays: the type of x is: <class 'float'> the type of n is: <class 'int'>

5. Multiple Assignment

The python language offers us the possibility to assign values to several variables at the same time


n, m = 10, 20
print("n = ", n)
print("m = ", m)
Which displays: n = 10 m = 20

6. Identity Of Variable Object

In Python, each created object uniquely identifies itself in Python. Python provides the guarantee that no two objects will have the same identifier. The built-in id() function is used to identify the object id. Consider the following example.


n = 10
print("Identifier of n: ", id(n))
print("ID of m: ", id(m))
# change the value of variable n
n = 20
print("Identifier of n: ", id(n))
Which displays: ID No: 8651024 m ID: 8651024 ID No: 8651184

7. Destroy a variable

To delete or destroy a variable in python, just use the del() function


del <variable_name>


n = 10
del n
Which displays the error message: builtins.NameError: name 'n' is not defined

Younes Derfoufi

No comments:

Post a Comment

Learn Python For Beginners

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (2) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (256) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) Bootstrap CSS (1) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (15) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) Github (2) HTML (9) IT News (3) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (37) Python BeautifulSoup (1) Python For Data Science (2) Python PyQt (13) Python Reference (1) Python Source Code (10) Python-Books (6) Python-DVD-Training (1) Python-Exercises (305) Python-Framework (1) Python-IDE (1) Python-Kivy-Framework (2) Python-Modules (1) Python-pdf (2) Python-pyQt (1) python-temp (3) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (4) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)

Sample text

Sample Text

Blogger Templates