Python variable program

                         Rajeev Ranjan Tiwari


Python Variables

Creating Variables

Variables are containers for storing data values.

Unlike other programming languages, Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

(1)  

x = 5

y = "Rajeev"

print(x)

print(y)

(2)
x = 4
x = "gorgeous"
print(x)


(3)
x = "John"
print(x)
#double quotes are the same as single quotes:
x = 'John'
print(x)

(4)
#Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"

#Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"

Assign Value to Multiple Variables

Python allows you to assign values to multiple variables in one line:

(5)
x, y, z = "GRAPS", "Apple", "mango"

print(x)
print(y)
print(z)


(6)
x = y = z = "Orange"

print(x)
print(y)
print(z)

(7) 
x = "nice Rajeev"
print("Python is " + x)

(8)
x = "Python is "
y = "good"
z = x + y
print(z)

(9)
x = 5
y = 10
print(x + y)

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.

(10)
x = "Rajeev Ranjan Tiwari"

def myfunc():
  print("Python is " + x)

myfunc()

(11)
x = "awesome"

def myfunc():
  x = "RAJEEV"
  print("Python is " + x)

myfunc()

print("Python is " + x)

(12)
def myfunc():
  global x
  x = "RAJEEV"

myfunc()

print("Python is " + x)

(13) 
x = "good"

def myfunc():
  global x
  x = "nice"

myfunc()

print("Python is " + x)



अंत मे अगर इस पोस्ट से आपको कुछ फायदा हुवा हो तो इसे दुसरो के साथ भी share करे ताकी दुसरो का भी फायदा हो. इस पोस्ट को पूरा पढ़ने के लिए आपका धन्यवाद और बहुत शुक्रिया.
Thanks friend posted by Rajeev Ranjan Tiwari

Comments

Post a Comment

Popular posts from this blog

OT Technician

D Pharma

Tor Browser