Python
Python string length | len() method Example
len() is a built-in function in python. You can use the len() to get the length of the given...
A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.
Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.
In this tutorial, we will learn,
Let see an example. We will define variable in Python and declare it as "a" and print it.
a=100 print (a)
You can re-declare Python variables even after you have declared once.
Here we have Python declare variable initialized to f=0.
Later, we re-assign the variable f to value "guru99"
Python 2 Example
# Declare a variable and initialize it f = 0 print f # re-declaring the variable works f = 'guru99' print f
Python 3 Example
# Declare a variable and initialize it f = 0 print(f) # re-declaring the variable works f = 'guru99' print(f)
Let's see whether you can concatenate different data types like string and number together. For example, we will concatenate "Guru" with the number "99".
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
For the following code, you will get undefined output -
a="Guru" b = 99 print a+b
Once the integer is declared as string, it can concatenate both "Guru" + str("99")= "Guru99" in the output.
a="Guru" b = 99 print(a+str(b))
There are two types of variables in Python, Global variable and Local variable. When you want to use the same variable for rest of your program or module you declare it as a global variable, while if you want to use the variable in a specific function or method, you use a local variable while Python variable declaration.
Let's understand this Python variable types with the difference between local and global variables in the below program.
Python 2 Example
# Declare a variable and initialize it f = 101 print f # Global vs. local variables in functions def someFunction(): # global f f = 'I am learning Python' print f someFunction() print f
Python 3 Example
# Declare a variable and initialize it f = 101 print(f) # Global vs. local variables in functions def someFunction(): # global f f = 'I am learning Python' print(f) someFunction() print(f)
While Python variable declaration using the keyword global, you can reference the global variable inside a function.
Python 2 Example
f = 101; print f # Global vs.local variables in functions def someFunction(): global f print f f = "changing global variable" someFunction() print f
Python 3 Example
f = 101; print(f) # Global vs.local variables in functions def someFunction(): global f print(f) f = "changing global variable" someFunction() print(f)
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.
Example of Python delete variable or Python clear variable :
f = 11; print(f) del f print(f)
len() is a built-in function in python. You can use the len() to get the length of the given...
What is Python String find()? Python String find() is a function available in Python library to find...
What is Function in Python? A Function in Python is a piece of code which runs when it is...
What is JSON? JSON is a standard format for data exchange, which is inspired by JavaScript....
Python Rename File Python rename() file is a method used to rename a file or a directory in Python...
Dictionary is one of the important data types available in Python. The data in a dictionary is...