Python Tutorial
Python Tutorial
a=100
print (a)
Example:
We will declare the following variable and print it.
Number= 25
Name = “Kiran”
B= 3.5
print (Number)
print(Name)
print (B)
2.Re-declare a Variable
You can re-declare Python variables even after you have declared once.
Example:
x = y = z = "SoftwareTestingHelp"
print (x)
print (y)
print (z)
Example:
a, b, c = 5, 3.2, "Hello"
print (a)
print (b)
print (c)
4. Print function
print("Hello World")
Example :
If you want to print the name of five countries, you can write:
print("USA")
print("Canada")
print("Germany")
print("France")
print("Japan")
5. How to print blank lines
Sometimes you need to print one blank line in your Python program. Following is
an example to perform this task using Python print format.
Example:
Let us print 8 blank lines. You can type:
print (8 * "\n")
or
print ("\n\n\n\n\n\n\n\n\n")
By default, print function in Python ends with a newline. This function comes with
a parameter called ‘end.’
Output:
Python@
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”.
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.
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
2. Variable f is again declared in function and assumes local scope. It is
assigned value “I am learning Python.” which is printed out as an output.
This Python declare variable is different from the global variable “f” defined
earlier
3. Once the function call is over, the local variable f is destroyed. At line 12,
when we again, print the value of “f” is it displays the value of global
variable f=101
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.
f = 11;
print(f)
del f
print(f)
10. Properties of variables
It can be only one word.
It can use only letters, numbers, and the underscore (_) character.
It can’t begin with a number.
Variable name starting with an underscore (_) are considered as “unuseful”.