0% found this document useful (0 votes)
2 views5 pages

Local and Global Variables in Python

Uploaded by

animetra14
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Local and Global Variables in Python

Uploaded by

animetra14
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

"""

# Local Vs Global Variables

Variables may be either global or local:

- Variables that are defined inside a function body have a local scope, and those defined outside
have a global scope.

- This means that local variables can be accessed only inside the function in which they are declared,
whereas global variables can be accessed throughout the program body by all functions.

### The global Keyword:

We only need to use the **global keyword** in a function if we want to assign or change the global
variable instead of creating a local variable.

global is not needed for printing and accessing.

Python “assumes” that we want a local variable due to the assignment to a variable inside of a
function. Any variable which is changed or created inside of a function is local if it hasn’t been
declared as a global variable. To tell Python, that we want to use the global variable, we have to use
the keyword **global**, as can be seen in the following example:

```python

a=1

# Uses global because there is no local 'a'

def f():

print('Inside f() : ', a)

# Variable 'a' is redefined as a local

def g():

a=2

print('Inside g() : ', a)


# Uses global keyword to modify global 'a'

def h():

global a

a=3

print('Inside h() : ', a)

# Global scope

print('global : ', a)

f()

print('global : ', a)

g()

print('global : ', a)

h()

print('global : ', a)

```

Q. write a code for making a calculator using python .

"""

"""

Typecasting in python

The conversion of one data type into the other data type is known as type casting

in python or type conversion in python.

Python supports a wide variety of functions or methods like:

int(), float(), str(), ord(), hex(), oct(), tuple(), set(), list(), dict(), etc.

for the type casting in python.


Two Types of Typecasting:

Explicit Conversion (Explicit type casting in python)

Implicit Conversion (Implicit type casting in python).

Explicit typecasting:

The conversion of one data type into another data type, done via developer or programmer's

intervention or manually as per the requirement, is known as explicit type conversion.

It can be achieved with the help of Pythons built-in type conversion functions such

as int(), float(), hex(), oct(), str(), etc .

"""

#example code

string = "15"

number = 7

string_number = int(string) #throws an error if the string is not a valid integer

sum= number + string_number

print("The Sum of both the numbers is: ", sum)

"""

Implicit type casting:

Data types in Python do not have the same level i.e. ordering of data types is not the

same in Python. Some of the data types have higher-order, and some have lower order.

While performing any operations on variables with different data types in Python, one of the

variable's data types will be changed to the higher data type. According to the level, one data

type is converted into other by the Python interpreter itself (automatically). This is called, implicit

typecasting in python.

Python converts a smaller data type to a higher data type to prevent data loss.

"""
# Python automatically converts

# a to int

a=7

print(type(a))

# Python automatically converts b to float

b = 3.0

print(type(b))

# Python automatically converts c to float as it is a float addition

c=a+b

print(c)

print(type(c))

"""

Taking User Input in python

In python, we can take user input directly by using input() function.

This input function gives a return value as string/character hence

we have to pass that into a variable

Syntax:

variable=input()

example :

variable=int(input())

variable=float(input())

"""

a = input("Enter your name: ")

print("My name is", a)


x = input("Enter first number: ")

y = input("Enter second number: ")

print(x + y)

print(int(x) + int(y))

You might also like