Subject : ADVANCED PROGRAMMING CONCEPTS - Python
To solve assignments Please join Google Classroom.
Invitation of Google Classroom has been sent to your official email id.
Please accept the google classroom invitation through official email id.
Or directly by entering Class code of Google Classroom also
you can join Google Class room to solve assignments.
Class code of Google Classroom : 3ojdsjm
With kind regards,
SD Bhosale
- 5 minutes before
To know attendance : Click here
Reference : https://www.w3schools.com/python/
Today's Learnings:
Unit No. 2 has been started.
python literal - Variables
Output:
Output:
Unit No. 2 has been started.
python literal - Variables
#docstring gets executed by python interpreter
a="""
Welcome to
SVERI Pandharpur
"""
print(a)
Output:
Welcome to
SVERI Pandharpur
# Python Variables# Creating Variables# Variables are containers
for storing data values.# Python has no command for declaring a
variable.
# Example
x = str(5.0)
y = 'Rahul'print("Value of x=",x)
print(y)
print("Datatype of the variable x:",type(x))
print("Datatype of the variable y:",type(y))
print(y+x)
Output:
Value of x= 5.0
Rahul
Datatype of the variable x: <class 'str'>
Datatype of the variable y: <class 'str'>
Rahul5.0
# Variables do not need to be declared with any particular# type and
can even change type after they have been set.
# Example
x = 4
# x is of type int
print(x,type(x))
x = "SVERI Pandharpur"
# x is now of type str
print(x,type(x))
4 <class 'int'>
SVERI Pandharpur <class 'str'>
"""String variables can be declared either by using single or
double quotes:
Example"""
x = "SVERI"
print("x=",x)
# is the same as
y = 'SVERI'
print('y=',y)
x= SVERI
y= SVERI
# Variable Names# A variable can have a short name (like x and y)
or a more
descriptive name (age, carname, total_volume).# Rules for Python
variables:# A variable name must start with a letter or the under
score
character# A variable name cannot start with a number# A variable
name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )# Variable names are
case-sensitive (age, Age and AGE are
three different variables)
#Legal variable names:
# myvar = "SVERI"
# my_var = "SVERI"
# _my_var = "SVERI"
# myVar = "SVERI"
# MYVAR = "SVERI"
# myvar2 = "SVERI"
#Illegal variable names:
#2myvar = "SVERI"
my-var = "SVERI"
#my var = "SVERI"
# Assign Value to Multiple Variables# Python allows you to assign
values to multiple# variables in one line:# Example
x, z, y, p = "Orange", "Banana", "Cherry",4
print("x=",x)
print("y=",y)
print("z=",z)
print("p=",p)
x="chiku"print("x=",x)
Output:
x= Orange
y= Cherry
z= Banana
p= 4
x= chiku
# And you can assign the same value to multiple variables in
one line:# Example
x = y = z = p= "Orange"
#x,y,z="orange" this is wrong!
print("x=",x)
print("y=",y)
print("z=",z)
print("p=",p)
x= Orange
y= Orange
z= Orange
p= Orange
x = "awesome"
print("Python is " + x)
Python is awesome
# You can also use the + character to add a variable to another
variable:# Example
x ,y= "Python is ","awesome!"
z = x + y
print(z)
Output:
Python is awesome!
# For numbers, the + character works as a mathematical operator:
# Example
x = '5'y = '10'
print(type(x + y))
x = 50
y = 1.0
print(type(x + y))
Output:
<class 'str'>
<class 'float'>
# If you try to combine a string and a number, Python will give you an
error:# Example
x = 5
y = "John"
print(x + y)
Output:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/python_variable_1/10.py", line 5, in <module>
print(x + y)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
# Global Variables# Variables that are created outside of a function
(as in all of the examples above)# are known as global variables.# Global variables can be used by everyone, both inside of
functions and outside.# Example# Create a variable outside of a function, and use it inside
the function
x = "awesome"
def myfunc():
x='No. 1' print("Python is " + x)
myfunc()
def fun():
print("SVERI is " + x)
fun()
print("Value of x=",x)
Output:
Python is No. 1
SVERI is awesome
Value of x= awesome
x = "awesome"def myfunc():
x = "fantastic" print("Python is " + x)
#print("Python is " + x)
myfunc()
def fun():
print("Python is " + x)
fun()
Output:
Python is fantastic
Python is awesome
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Output:
Python is fantastic
# Also, use the global keyword if you want to change a global
variable inside a function.# Example# To change the value of
a global variable inside a function,
refer to the variable# by using the global keyword:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Output:
Python is fantastic
Google Meet Session Video:
No comments:
Post a Comment