Tuesday, April 26, 2022

Python Variables


 Python Variables



     Variables are reserved memory locations to store values. We can store values like numbers, strings, float etc in computer memory while executing a program using containers called variables.

    In simple, if we have a book in a box, then box is a variable and book is stored values.

Rules for naming a variables:

1. Python Variables start with either Upper case (A-Z) or Lower case (a-z) or Underscore character(_). Example: _myvalue, myalue, Myvalue…

2. We can use digits (0-9), but we cant start a variable with digit. Example: 1count, 1value are invalid variables. My1value, my_value1 are valid variables.

3. Variables are case sensitive. Example: myname and Myname are different variables.


Example Programs to understand use of Python Variables better:

1. Program for using different variables with same name:

myname="Python"

print(myname)

myName="coding"

print(myName)

Myname="doit"

print(Myname)


Output:

Python

coding

doit

 

2. Program to find type of a variable:

a="Python"

print(type(a))

a=10

print(type(a))

a=10.24

print(type(a))

a=True

print(type(a))


Output

<class 'str'>

<class 'int'>

<class 'float'>

<class 'bool'>

 

3. Program to assign same values to different variables:

a=20

b=20

c=20

print(a)

print(b)

print(c)


Output

20

20

20


4. Program to assign same values to different variables in single line

a=b=c=20

print(a)

print(b)

print(c)


Output

20

20

20


5. Program to assign different values to different variable:

x=10

y=20

z=30

print(x)

print(y)

print(z)


Output

10

20

30


6. Program to assign different values to different variable in single Line:

x,y,z=10,20,30

print(x)

print(y)

print(z)


Output

10

20

30


7. Program to assign different values to different variable and print different values in single line

x,y,z=10,20,30

print(x,y,z)


Output

10 20 30


8. Program to assign different String values to different variable

x,y,z="Apple","Boy","Cat"

print(x)

print(y)

print(z)

 

Output

Apple

Boy

Cat


9. Program to assign multiple strings to variable:

x="Hello Viewers, Welcome"

print(x)


Output

Hello Viewers, Welcome

 

10. Program to assign different type values to different variable:

a,b,c="10","20.45", "Hello"

print(a,b,c)


Output

10   20.45   Hello


 Thanks and Regards

J.Divyabharathi Veera

No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...