Why indentation is important in Python and what is the scope of a global variable?

Normally, in every programming language we should normally use indentation in order to have a nice style and a readable code. But Python programs are structured through indentation, so different code blocks are defined by indentation. This indentation makes the code easier to read and understand. So all statements that are indexed with the same number of lines are part of the same code blocks.
A global variable in Python is the variable which is declared and defined outside a function.For example:def printing(): print variable

#Here we have the global scope defined variable = "I love python" printing()

#This code will print: I love python
But now, if we want to declare a varible with the same name as the global one, we have the following:
def printing(): variable = "I like python" print variable

#Here we have the global scope defined variable = "I love python" printing() print variable

#This code will output "I like python" and the "I love python" because they are completely different variables (in different scopes) but with the same name

GP
Answered by George-Liviu P. Computing tutor

2288 Views

See similar Computing A Level tutors

Related Computing A Level answers

All answers ▸

How does the Stack data type work?


When a stack is implemented using a 1-D array, adding a valid item can cause an execution error. Explain why an execution error can occur in this situation.


Calculate -3 + 3 using Two's Complement and the Sign and Magnitude Method. Which works correctly?


Write a function in pseudocode that determines if a given string is a palindrome


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences