Python Basics3
Python Basics3
Table of Contents:
1 a=100
2 print (a)
https://www.guru99.com/variables-in-python.html 1/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Run
Re-declare a Variable
You can re-declare Python variables even after you have declared once.
Python 2 Example
This code is editable. Click Run to Execute
Run
Python 3 Example
This code is editable. Click Run to Execute
Run
RELATED ARTICLES
→ Python Lambda Functions with EXAMPLES
→ Python readline() Method with Examples
→ Python break, continue, pass statements with Examples
→ How to reverse a String in Python (5 Methods)
https://www.guru99.com/variables-in-python.html 3/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Unlike Java, which concatenates number with string without declaring number as
string, while declaring variables in Python requires declaring the number as string
otherwise it will show a TypeError
1 a="Guru"
2 b = 99
3 print a+b
4
https://www.guru99.com/variables-in-python.html 4/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Run
Once the integer is declared as string, it can concatenate both “Guru” + str(“99”)=
“Guru99” in the output.
1 a="Guru"
2 b = 99
3 print(a+str(b))
Run
Let’s understand this Python variable types with the difference between local and
global variables in the below program.
1. Let us define variable in Python where the variable “f” is global in scope and is
assigned value 101 which is printed in output
https://www.guru99.com/variables-in-python.html 5/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Python 2 Example
This code is editable. Click Run to Execute
https://www.guru99.com/variables-in-python.html 6/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Run
Python 3 Example
This code is editable. Click Run to Execute
Run
While Python variable declaration using the keyword global, you can reference the
global variable inside a function.
1. Variable “f” is global in scope and is assigned value 101 which is printed in
output
2. Variable f is declared using the keyword global. This is NOT a local variable,
but the same global variable declared earlier. Hence when we print its value,
the output is 101
3. We changed the value of “f” inside the function. Once the function call is over,
the changed value of the variable “f” persists. At line 12, when we again, print
the value of “f” is it displays the value “changing global variable”
https://www.guru99.com/variables-in-python.html 7/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Python 2 Example
This code is editable. Click Run to Execute
1 f = 101;
2 print f
3 # Global vs.local variables in functions
4 def someFunction():
5 global f
6 print f
7 f = "changing global variable"
8 someFunction()
9 print f
Run
https://www.guru99.com/variables-in-python.html 8/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Python 3 Example
This code is editable. Click Run to Execute
1 f = 101;
2 print(f)
3 # Global vs.local variables in functions
4 def someFunction():
5 global f
6 print(f)
7 f = "changing global variable"
8 someFunction()
9 print(f)
Run
Delete a variable
You can also delete Python variables using the command del “variable name”.
In the below example of Python delete variable, we deleted variable f, and when we
proceed to print it, we get error “variable name is not defined” which means you
have deleted the variable.
https://www.guru99.com/variables-in-python.html 9/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
1 f = 11;
2 print(f)
3 del f
4 print(f)
Run
Summary
https://www.guru99.com/variables-in-python.html 10/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Top
Top Python Python
Python TUPLE
TUPLE - 15 BEST Python
Interview Pack,
Pack, Unpack, Courses Online
Questions and… Compare,…
Compare,… for…
Dictionary
Dictionary in
Python
Python with
Syntax & Example
https://www.guru99.com/variables-in-python.html 11/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
Do you think
AI is future of
Work?
Yes No
About
About Us
Advertise with Us
Contact Us
Career Suggestion
SAP Career Suggestion Tool
Software Testing as a Career
Xero
RemotePC
QuickBooks
Interesting
eBook
Blog
Quiz
https://www.guru99.com/variables-in-python.html 12/13
12/13/24, 11:29 PM Python Variables: How to Define/Declare String Variable Types
SAP eBook
Privacy Manager
https://www.guru99.com/variables-in-python.html 13/13