Python
Python
python
[Date]
sekhar sampangi
[company name]
Creating variables
Python has no command for a variable.
A variable is created the moment you first assign
Ex:-
x=5
y=”john”
output:- 5
john
Variables do not need to be declared with any particular type and can
even changed type after have been set.
X=4 # x is a type int
X=”sally” # x is a now of type str
Print(x)
Output :- sally
Casting :- if you to specify the data type of a variable, this can be done
with casing
Ex:-
X=str(3) # x will be 3
Y=int(3) # y will be 3
Z=float(3) # Z will be 3.0
Out put:- 3
3
3.0
Get the type:-
You can get the data type of a variable with the type ( ) function
X=5
Y=5
Print (type (x))
Print(type (y))
<class ‘int’>
<class ‘str’>
Examples:-
myvar=
my-var=
-my-var=
myvar=
MYVAR=
myvar2=
unpack a collection: -
if you have a collection of values in alist tuple etc. python allows you to extract
the values into variables this is called unpacking
Examples: -
Un pack list
Fruits = [“apple”,“banana”,“cherry”]
x,y,z=fruits
output:-
print(x)
print(y)
print(z)
out put variables:-
the python print() function is often used to output variables
examples:-
x=”python is awesome”
print(x)
In the print () function you output multiple variables,separated by a
comma
Example:-
x=”python”
y=”is”
z=”awesome”
print(x,y,z)
you can also use the +operator to output multiple variable :
x = “python”
y = ‘is”
z = “awesome”
print (x+y+z)
for numbers the + characters works as a mathematical operator:
Example: -
x =5
y =10
Print (x+y)=15
In the print ( ) functions, when you try to combine a string and a number
with the + operator, python will give you on error
Example: -
x =5
y =”john”
Print (x+y)
Out put :- error
The best way to output multiple variables in the print ( ) function is to
separate in the print() function is to separate them with comes, with even
support different data type.
Examples: -
x =5
y =”john”
Print (x,y)
Output :- 5 john
Global variables: -
Variables that are created outside of a function las in all of the examples
in the previous pages are known as gloabal variables
Global variables can be used by everyone, both inside of functiond and
outsides
Examples: -
Create a variable outside of a functions and use it inside the fuction
x=”awesome”
def my fun():
print (“python is” + x)
mt func ()
output: - python is awesome
if you create a variable with the same name inside a function, this
variables will be local and can only be used inside the function the global
variable with the same name will remain as , it was, global and with the original
value
Example:- create a variable inside a function, with the same name as the global
variable
x=”awesome”
def my func ():
=”fantastic”
Print (“python is “+x)
My func()
Print(”python is”+x)
Python is fantastic
Python is awesome
Example: -
If you use the global keyword the belongs to the global scope
def my func();
global x
x =”fantastic”
my func()
print(“python is”+x)
python is fantastic
Example:-
To change the value of a global variable inside a function, refer to the variable
by using the global keyword
X=”awesome”
de my func():
global x
x=”fantastic”
my func()
print (“python is “ +x)
python is fantastic
1. Arithmetic
2. Assignment
3. Comparision
4. Logical
5. Identity
6. Membership
7. Bitwise
Arithmetic operators
Operator
+ Addition
- subtraction
* multiplication
/ divisions
% modulus divisions
** exponentiation
// floor division
x=15
y=2
print=(x/y)