0% found this document useful (0 votes)
0 views

python 2

The document explains Python variables, including their definition, types, and naming conventions. It distinguishes between local and global variables, providing examples for each type. Additionally, it discusses best practices for naming multi-word variables to enhance readability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

python 2

The document explains Python variables, including their definition, types, and naming conventions. It distinguishes between local and global variables, providing examples for each type. Additionally, it discusses best practices for naming multi-word variables to enhance readability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

</> CodeWithHarry Menu Login

This site uses Google AdSense ad intent links. AdSense automatically generates these links and they may help creators earn
money.

Python Variables
Variables are containers that store information that can be manipulated and
referenced later by the programmer within the code.
In python, the programmer does not need to declare the variable type explicitly,
we just need to assign the value to the variable.
Example:

name = "Abhishek" #type str


age = 20 #type int
passed = True #type bool

It is always advisable to keep variable names descriptive and to follow a set of


conventions while creating variables:
Variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _ )
Variable name must start with a letter or the underscore character.
Variables are case sensitive.
Variable name cannot start with a number.
Example:

Color = "yellow" #valid variable name


cOlor = "red" #valid variable name
_color = "blue" #valid variable name

5color = "green" #invalid variable name


$color = "orange" #invalid variable name

Sometimes, a multi-word variable name can be difficult to read by the reader. To


make it more readable, the programmer can do the following:
Example:
NameOfCity = "Mumbai" #Pascal case
nameOfCity = "Berlin" #Camel case
name_of_city = "Moscow" #snake case

Scope of variable:
The scope of the variable is the area within which the variable has been created.
Based on this a variable can either have a local scope or a global scope.

Local Variable:
A local variable is created within a function and can be only used inside that
function. Such a variable has a local scope.
Example:

icecream = "Vanilla" #global variable


def foods():
vegetable = "Potato" #local variable
fruit = "Lichi" #local variable
print(vegetable + " is a local variable value.")
print(icecream + " is a global variable value.")
print(fruit + " is a local variable value.")

foods()

Output:

Potato is a local variable value.


Vanilla is a global variable value.
Lichi is a local variable value.

Global Variable:
A global variable is created in the main body of the code and can be used anywhere
within the code. Such a variable has a global scope.
Example:

icecream = "Vanilla" #global variable


def foods():
vegetable = "Potato" #local variable
fruit = "Lichi" #local variable
print(vegetable + " is a local variable value.")

foods()
print(icecream + " is a global variable value.")
print(fruit + " is a local variable value.")

Output:

Potato is a local variable value.


Vanilla is a global variable value.

< < Previous Next > >

CodeWithHarry Copyright © 2024 CodeWithHarry.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy