0% found this document useful (0 votes)
17 views

Python

Hi

Uploaded by

sekharsekhar2049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Python

Hi

Uploaded by

sekharsekhar2049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

21-06-2024

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’>

Single or double quotes


String variables can be declared either by Using single or double quotes
X=”john”
#is the same as
X=’john’
Case-sensitive
Variables names are case-sensitive
Ex:-
a=4
A = ”sally”
# A will not over write a
Print (a)
Print(A)
Out put:-
a=4
A = sally
Variable Names
A variables can have a short name (like x and y) or a more descripute name
(age,carname,total-volume).rules for python variables:
 A variables name starts with a letter or the underscore character
 A variables name cannot start with a number
 A variables name can only contain alph-numeric characters and under
scores (A-Z,0-9,and _)
 Variables names are case-sensitive (age , Age and AGE are three different
variables)
 A variables name cannot be any of the python keywords.

Examples:-
myvar=
my-var=
-my-var=
myvar=
MYVAR=
myvar2=

Multi words variables Names


Variables name with more than one word can be difficult to read
There are sereral technious you can use to make them more readable
Camel case:-
Each word, ”except the first ,” starts with a capital letter
My variables Nam = “john”
pascal case:-
each word starts with a capital letter
My Variable Name =”john”
Snake case:-
Each word is separated by an under score character
my_variable_name =”john”

Assign multiple values


Many values to multiple variables
 Python allows you to assign values to multiple variable inn one line
Example:-
x,y,z =”orange”,”banana”,”cherry”
print(x)
print(y)
print(z)
orange
banana
cherry
 And you can assign the same value to multiple variables in one lines
Examples:-
x=y=z”orange”
print(x)
print(y)
print(z)
out put :-
orange
orange
orange

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

The global keyword


Normally when you create a variable inside a function that variable is local and
can only be used inside that function.
To create a global variable inside a function you can use the global
keyboard

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

python operators format apply or remove

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

a =int (input(enter a value”))


b =int (input(enter b value”))
c=a+b; d=a-b; e=a*b; f=a/b; g=a%b;

print(“addition value is:”, c)


print(“subtraction value is:”, d)
print(“multiplication value is:”, e)
print(“divisions value is:”, f)
print(“remainder value is:”, g)
out put :-
enter a value : 10
enter b value : 2
addition value :12
substruction value :8
multiplication value :20
divisions value :5
remainder value :0

x=15
y=2
print=(x/y)

You might also like