Tuesday, November 19, 2019

Variable in Python.


Variable/Label in Python


  • Definition: Named location that refers to a value and whose value can be used and processed during program execution.
  • Variables in python do not have fixed locations. The location they refer to changes every time their values change.

Creating a variable: 

  • A variable is created the moment you first assign a value to it. 
  • Example: x = 5 y = “hello” Variables do not need to be declared with any particular type and can even change type after they have been set. 
  • It is known as dynamic Typing. x = 4 # x is of type int x = "python" # x is now of type str print(x) 

Rules for Python variables: 

  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscore (A-z, 0-9, and _ ).
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • Python allows assign a single value to multiple variables. Example: x = y = z = 5 You can also assign multiple values to multiple variables. 
  • For example −

x , y , z = 4, 5, “python”
4 is assigned to x, 5 is assigned to y and string “python” assigned to variable z respectively.
x=12
y=14
x,y=y,x
print(x,y)
Now the result will be
14 12

Lvalue and Rvalue:


  • An expression has two values. Lvalue and Rvalue.
  • Lvalue: the LHS part of the expression.
  • Rvalue: the RHS part of the expression.
  • Python first evaluates the RHS expression and then assigns to LHS.

Example:
p, q, r= 5, 10, 7
q, r, p = p+1, q+2, r-1
print (p,q,r)
Now the result will be:
6  6  12
Note: Expressions separated with commas are evaluated from left to right and assigned in same order.

  • If you want to know the type of variable, you can use type( ) function :

Syntax:
        type (variable-name)
  Example:
                     x=6
                     type(x)
The result will be:
<class ‘int’>

  • If you want to know the memory address or location of the object, you can use id( ) function.

Example:
>>>id(5)
1561184448
>>>b=5
>>>id(b)
1561184448

  • You can delete single or multiple variables by using del statement. 
Example:
del x
del y, z

Input from a user:


  • input( ) method is used to take input from the user.

Example: 
print("Enter your name:") x = input( ) print("Hello, " + x)
  • input() function always returns a value of string type.

Type Casting:

  • To convert one data type into another data type. Casting in python is therefore done using constructor functions.
int( ) - constructs an integer number from an integer literal, a float literal or a string literal. 
Example: 
                  x = int(1) # x will be 1 
                  y = int(2.8) # y will be 2 
                  z = int("3") # z will be 3.
float( ) - constructs a float number from an integer literal, a float literal or a string literal. Example: 
                   x = float(1) # x will be 1.0 
                   y = float(2.8) # y will be 2.8 
                   z = float("3") # z will be 3.0 
                   w = float("4.2") # w will be 4.2.

str( ) - constructs a string from a wide variety of data types, including strings, integer literals and float literals. 
Example: 
                   x = str("s1") # x will be 's1' 
                   y = str(2) # y will be '2' 
                   z = str(3.0) # z will be '3.0'


Reading a number from a user:

x= int (input(“Enter an integer number”))
2.8 OUTPUT using print( ) statement:
Syntax:
print(object, sep=<separator string >, end=<end-string>)

  • object : It can be one or multiple objects separated by comma.
  • sep : sep argument specifies the separator character or string. It separate the objects/items. By default sep argument adds space in between the items when printing.
  • end : It determines the end character that will be printed at the end of print line. By default it has newline character( ‘\n’ ).

Example:
x=10
y=20
z=30
print(x,y,z, sep=’@’, end= ‘ ‘)
Output:
10@20@30

No comments:
Write comments