Showing posts with label Python: Variables in Python. Show all posts
Showing posts with label Python: Variables in Python. Show all posts

Python: Variable

Variables:

 Variable is the name or a holder of a value, stored in a particular memory location or we can say that variable is the name of particular memory location storing a value, the value can be changed anytime.

In Python, we don't need to specify the types of a variable, because Python is an infer language and it is smart enough to determine the type of a variable.

Identifiers:

Identifiers are things like variables. An identifier is utilized to recognize the literals utilized in the program.

Here are some rules to name the variables/identifiers:

  • The variable's first character must be an alphabet or underscore(_).
  • They can be a group of both letters and digits.
  • The name of the variable should be written in lowercase. 
  • Name of the variables/identifiers are case sensitive. Both A and a are distinct variables. In same way the firstname   and   FirstName both are different.
  • White space and special characters (@, #, $, %, ^, etc) are not allowed in identifier's name.
  • Identifier's name should not be any watchword characterized in the language.
Some examples of valid identifiers are: xyz, _ab, m_n, a123 etc.
Some examples of invalid identifiers are: 1x, n%9, z 10 etc.

Declare variable and assign value:

  • In Python, the variable is declaired automatically whenever a value is added to it, we don't have to declare the variables specially.
  • It permits us to make a variable at the necessary time. We don't have to make a variable prior to involve it in application.
  • The equal(=) operator is used to assign a value to a variable.

Object References:

Compared to a lot of other programming languages, the procedure for dealing with variables is a little different in Pyhton.

Python is an object oriented programming language. Because of this every data item is a part of a particular class.

Let's see a program:

print("hello")

The output is

hello

The python object makes an integer object and shows it to the control center. We have created a string object with print statement above. Now we use built in type() function in Python to determine its type.

Let see:

type("Hello")

The output is

<class 'str'>

Here output shows that the class is string.

Factors:

In Python factors are an symbolic name that is reference to an item. The factors are utilized to indicate objects by that name.
Let's see:
a = 50
In above example, the variable 'a' refers to an integer object.
If we assign this integer value 50 to another variable 'b'.
We write like:
a = 50
b = a
a = 50 = b
The variable 'b' refers to the same object, that is refered by 'a' also. Here Python doesn't create another object. Let's assign the new value to 'b'. 
a = 50
b = 100
Now both variables 'a' and 'b' refer to different objects.
Python manages memory effeciently, when we assign the same variables to two different value.

Object Identity:

Every object created in Python has a unique identifier. Python gives the dependable that no two items will have a similar identifier. The object identifier is identified using the built in id() function.

Let's see the example:

a = 10

b = a

print(id(a))

print (id(b))

# Now we reassign the variable a

a = 100

print(id(a))


The output is

140717161505864

140717161505864

140717161508744

In above example, We assigned b=a, so a and b both having similar values. The id() function also returned the same number for both, but when we reassign variable a = 100 then new object identifier is returned by id() function.

Variable Names: The process for declaring the valid variable has already been discussed. Variable names can be any length, can have capitalized, lower case (a-z), digit (0-9) and underscore (_). Take a look at the names of valid variables in the following example.

name="Suman"

age=23

marks=90

print(name)

print(age)

print(marks)


Output

Suman

23

90

Multi word variable: We can keep the multi word variable (like First_Name, lastName) by following ways:

Camel Case: In camel case, word or abbreviation in middle will start with capital letter. Like nameOfStudents, rollNumber etc.

Pascal Case: It is same as Camel case but here first letter is also capital. Like NameOfStudents, RollNumber etc

Snake Case: In the snake case words are separated ny underscore. Like name_of_students, roll_number etc.

Multiple Assignment:

This feature is found in Python. In multgiple assignment different  values are or one value is assigned to multiple variables in one statement. Like

Assigning one value to multiple variables

Statement is

a=b=c=10

print(a)

print(b)

print(c)


Output is

10

10

10

Assigning different values to multiple variables

Statement is

a,b,c=10,20,30
print a
print b
print c

Output is

10

20

30

Variable Types in Python:
There are two types of variables in Python. Local and Global variable. Let's understand the following variables.

Local Variable: The variable that are declaired within the function and have scope within the function are known as local variable. 
Let's see a program
# declairing a function
def add():

# defining local variable. They scope within the function only.
a = 10
b = 20
c = a+b
print("The vsum is:", c)

#Calling a function
add()

Output is

The sum is: 30

In above program we declaired the function add() and assigned a few variable to it. These factors are treated as neibourhood factors which have scope just inside the capability. We get the error, if we attempt it outside the function.
Let's see
add()
# Accessing local variable outside the function.
print(a)

Output is
The sum is: 30
print(a)
Name Error: name 'a' is not defined.

Here we tried the local variable (a) outside the scope so we get error message.

Global Variable: Global variable can be used inside or outside the function and it can be utilized all through the program.
By default a variable declaired outside the function is global variable. It will work like local variable, if we don't use global keyword to declaire it as global variable.
Let's see an example:
  # declaire a variable
x=10

# Global variable in function
def myfunction():

#printing a global variable
global x
print(x)


#Modification in global variable
x='hello world'
print(x)
myfunction()
print(x)

Output is
10
hello world
hello world

In above code we declaired a variable x and gave a value 10 to it. Then we created a function and used global keyword to access the declaired variable within the function. After that we changed the value and gave a new string value to variable x. Then called the function and printed x, now it will display the new value.    

Deleting a variable:
We can delete a variable using 'del' keyword.
The syntax is 
del <variable_name>
Let's see a program

# Assigning a value to x
x=10
print(x)
# deleting the variable x
del x
print(x)

Output is    
10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined       

Maximum possible value of an integer in Python:
Python uses only int data type to handle all integer value. We can store long as well as short integer only in int data type.
Let's see a program  :

#A Python program to display large number
x=10000000000000000000000000000000
x=x+1
print(type(x))
print(x)

Output is
<class 'int'>
10000000000000000000000000000001

In above program we can see, Python does not use special data type for storing large number.

We can display more than one values with single print statement.
Let's see a program

#Using multiple values with single print statement
x=10
y=20
print(x, y)
print(1, 2, 3, 4, 5, 6, 7, 8)

The output is
10 20
1 2 3 4 5 6 7 8

Exercise:
1. What is a memory location in a computer that holds value called?
2. Define variables. How is a value assigned to a variable?
3. State whether the following variable names are valid or invalid in Python. Explain reasons too.
i. print
ii. first_firstname
iii. 123smart
iv. USER NAME
v. Comp$Science
 

                                                                                                   
                                                                                                                                                                                                                                                                                                                                                                           

Python: Functions