alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Python - Things to know before proceed - Variable Tutorial

Python variable is a container in memory (storage area) to hold value or data. In python, there is no need to assign a variable with datatype because python can detect the data type of a variable automatically.

We can assign a value to a variable by simply using the assignment operator (=).

For example-

a=10

print(a)

b= 'fresherbell.com'

print(b)

 

Run Program

Here we have created a variable named a and b with no datatype and assigned the value integer 10 to variable a and string “fresherbell.com” to variable b.

 

Rules and Naming Convention for Variables

  • Variable can contain a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). For example-
a_A1=10

print(a_A1)

Run Program

  • Variable name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
  • The variable name must not be similar to any keyword defined in the language.
  • The variable name is case sensitive, i.e a and A has a different meaning.

Assigning a single value to multiple variable

a=b=c=10

print(a)

print(b)

print(c)

Run Program

Assigning multiple values to multiple variable

a,b,c=10,20,30

print(a)

print(b)

print(c)

Run Program

 

Type of Variable in Python

  • Local Variable

A local variable is a variable that is declared inside the function and has scope inside the function.

def localvariable():
    
    a=10
    
    print (a)
    
localvariable()
#It will give error - name 'a' is not defined
print(a) 

Run Program

 

In the above example, we declare a local variable inside function localvariable(), which will print a value of 10 on calling a function localvariable().

Also, we tried to print variable outside function localvariable(), which will give an error that variable a is not defined.

Hence local variables will only work within its scope.

  • Global Variable

A global variable can be used anywhere in the program. Its scope is within the entire program.

The variable outside function is by default global. To declare a global variable inside a function use the global keyword behind the variable.

a=20 # Global Variable
def localvariable():
    global b
    b=10
    print (b)
localvariable()
print(a)
print(b)

 

Run Program

Constants

A constant is a type of variable, whose value cannot be changed. Constant is always written in CAPS or UpperCase.

ABC = 10
PI = 3.14

 

Python

Python

  • Introduction
  • Installation and running a first python program
    • How to install Python ?
    • Running 1st Hello World Python Program
    • How to install Pycharm ?
  • Things to know before proceed
    • Escape Characters/Special Characters
    • Syntax ( Indentation, Comments )
    • Variable
    • Datatype
    • Keyword
    • Literals
    • Operator
    • Precedence & Associativity Of Operator
    • Identifiers
    • Ascii, Binary, Octal, Hexadecimal
    • TypeCasting
    • Input, Output Function and Formatting
  • Decision control and looping statement
    • if-else
    • for loop
    • While loop
    • Break Statement
    • Continue Statement
    • Pass Statement
  • Datatype
    • Number
    • List
    • Tuple
    • Dictionary
    • String
    • Set
    • None & Boolean
  • String Method
    • capitalize()
    • upper()
    • lower()
    • swapcase()
    • casefold()
    • count()
    • center()
    • endswith()
    • split()
    • rsplit()
    • title()
    • strip()
    • lstrip()
    • rstrip()
    • find()
    • index()
    • format()
    • encode()
    • expandtabs()
    • format_map()
    • join()
    • ljust()
    • rjust()
    • maketrans()
    • partition()
    • rpartition()
    • translate()
    • splitlines()
    • zfill()
    • isalpha()
    • isalnum()
    • isdecimal()
    • isdigit()
    • isidentifier()
    • islower()
    • isupper()
    • isnumeric()
    • isprintable()
    • isspace()
    • istitle()
  • Python Function
    • Introduction
  • Lambda Function
    • Lambda Function
  • Python Advanced
    • Iterators
    • Module
    • File Handling
    • Exceptional Handling
    • RegEx - Regular Expression
    • Numpy
    • Pandas

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.

Toggle