• Home
  • Blog
  • Our Services
  • Contact Us
  • Register
Have any question?

(+91) 844 745 8168
[email protected]”>
RegisterLogin
AI Next Generation
  • Home
  • Blog
  • Our Services
  • Contact Us
  • Register

Python

  • Home
  • Blog
  • Python
  • Variable in Python

Variable in Python

  • Posted by Rehan
  • Date August 25, 2021
  • Comments 0 comment

Introduction

We will start with Variable in Python before starting this blog you must be clear in basic concepts of Python https://ainewgeneration.com/introduction-to-python/. So what is variable? The word variable we used in mathematics concepts for Algebra. Where we are using x & y as variables, let x be 5 and y be 6 in Algebra. The same concepts arise in a programming language, in which a variable is used to store some value. Let’s get started with more details about the python variable.

Table of Contents

  1. What is Variable ?
    • Declaring variable
    • Rules for Variable Naming
  2. Assigning Values
    • Single Assignment
    • Multiple Assignment
  3. Python Variable Types
    • Local Variable
    • Global Variable

What is Variable ?

A variable is a container where you can put your values for example in below image 2 is value and where 2 is store in container name as x.

Source: Telusko

Declaring Variable

We don’t need to declare the type of variable. python automatic detects what type of value you are assigning. for example :

  • x= 2 (python automatic declare it is an integer)
  • x = 2.3 (python automatic declare it is an float)
  • x = 2i+3j (python automatic declare it is an complex)
  • x = “AI Next Generation” (python automatic declare it is an string)

Rules for Variable Naming

There are some rules given below when you want to declare a variable name :

  • The first character of the variable should be alphabetical or underscore (_).
  • All characters except the first character can be alphabetical alphabet (a-z), uppercase (A-Z), underscore, or digit (0-9).
  • The variable name must not contain a white space, or a special character (!, @, #,%, ^, &, *).
  • The variable name must not be the same as any keyword defined in the language.
  • The variable names are sensitive; for example, ainextgeneration, and AINextGeneration is not the same.

Assigning Values

The equivalent operator (=) is used for variable conversion i.e equal operator “=” just assign value to a variable.

Single Assignment

We have already seen above about a single assignment if you want to store or assign any value in a single variable. for example : a = 50 , b =100, z = “AI Next Generation” etc.

Python variables - javatpoint
Source: Google Search Image

Multiple Assignment

We can store or assign any single value in multiple variables or multiple values in multiple variables which are known as multiple assignments. let’s understand with example with single and multiple assignments.

  • Assigning single value to multiple variables :
a = b = c = 100
print("a:" ,a)
print("b:" ,b)
print("c:" ,c)

output: a : 100
        b : 100
        c : 100
  • Assigning multiple values to multiples variables :
a,b,c = 10 ,20 ,30
print("a:" ,a)
print("b:" ,b)
print("c:" ,c)

output: a : 10
        b : 20
        c : 30

Python Variable Types

There are 2 types of variables: Local Variable and Global Variable we will understand the basic difference between them with an example using function.

Local Variable

Local variables are declared within the function and are wide within the function. let’s understand with an example in the below code using function.

# Declaring a function
def my_name():
#defining a local variable inside function 
    age = 22
    n = "my name is AI Next Generation"
    print(n," ,my age is :" ,age)

#calling my_name function
my_name()

output:my name is AI Next Generation,my age is:22

In the above, we build a function my_name() where age and n are the local variables we have used so they are known as local variables as they can’t be used outside of function as it will display an error i.e variable name is not defined.

Global Variable

Global Variable is declared anywhere as whenever you are defining a variable name outside of function the variable automatic becomes a global variable and you can use a variable that within function too but if you want to declare any variable name inside the function python provides a specific keyword i.e Global let’s understand with code given below :

#Declare a variable
x = 100

def my_name():
    #Declare a global variable 
     global x
     print(x)
     #modify a global variable 
     x = "AI Next Generation"
     print(x)

#call function my_name()
my_name()
print(x)

output: 100
        AI Next Generation
        AI Next Generation

In the above code, we declared x is a global variable and function my_name() we create global variable x by using keyword i.e global now we can modify the variable x and assigned other value to it.

End Notes

I hope this blog variable in python is much clear from basic to advanced In the next blog, we will focus on the list in python.

Tag:python

  • Share:
author avatar
Rehan

Previous post

Introduction to Python
August 25, 2021

Next post

YOLO - Object Detection
August 25, 2021

You may also like

1_5UHmgCaTD8EegsPuKcxC1Q
K-Means Clustering in Machine Learning
19 September, 2021
gettyimages-1284922959 (6)
Inheritance in Python
10 September, 2021
gettyimages-1284922959 (5)
Types of Variables & Methods in Python OOPs
9 September, 2021

Leave A Reply Cancel reply

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

Recent Posts

  • Garbage Classification using CNN Model
  • Brain Tumor Prediction PyTorch[CNN]
  • Covid-19 X-ray prediction using Deep Learning
  • Data Analysis for Olympic 2021
  • Naive Bayes in Machine Learning

Categories

  • Data Science
  • Deep Learning
  • Machine Learning
  • Python

Archives

  • December 2021
  • November 2021
  • September 2021
  • August 2021
  • July 2021

(+91) 844 745 8168

[email protected]

COMPANY

  • Blog
  • Our Services
  • Contact Us

LINKS

  • Home
  • Blog
  • Activity
  • Checkout

RECOMMEND

  • Cart
  • Members
  • Sample Page
  • Shop

SUPPORT

  • Members
  • My account
  • Register
  • Shop

Copyright © 2021 AI New Generation

Become an instructor?

Join thousand of instructors and earn money hassle free!

Get started now

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now