### Unit 1 - Variables and Manipulating Methods

CBSE Revision Notes

Class-11 Computer Science (New Syllabus)
Unit 1: Programming and Computational Thinking (PCT-1)

Variables and Manipulating Methods

Variable is a name assigned to a memory location to store the data. The data that a variable can store depends on data type for example

X=20
y=20.5
z=”Hello”

Here x, y and z are name of three variables all of them are holding different data depending on their data type e.g x is holding integer type data y is holding float type and z is holding string data. Basically all this data is stored in memory and it’s accessed by its name called variables. Variables are nothing but name given to a memory location or we can say it is a reference to a memory location where the data is stored we can easily access the data from memory using these variables.

Variables need not be declared first in python. They can be used directly. Variables in python are case sensitive.

Scope of a variable: It means where we can access the variable in a program e.g if we have defined a variable inside a function we cannot access it outside that function. We can say scope of this variable is inside the function. Scope of a variable can be local or global.

Local variable: A variable is said to be local variable if variable is declared inside the block of function then that variable cannot be accessed outside that block or function e.g.

1. def fun():
2. s=10
3. print(s)
4. return
5. print(s)
6. fun()

At line no 1 we have defined a function and have declared variable inside that namely s function ends at line no 4. At line no 5 when we try to access the variable s outside its block it will give an error at line no 6 function is called and it will display result of s i.e. 10.

Global variable: Scope of a global variable is throughout the program global variable is not defined inside any block and can be accessed anywhere e.g.

1. s=10
2. def fun():
3. print(s)
4. return
5. print(s)
6. fun()

At line no 1 s is defined as a global variable and we can access it inside the function and outside the function that means its scope is throughout the program.

Methods to manipulate variables: We have different types of methods to manipulate variables in python. Methods depends on the data type we are using. There are methods for number type of data like

abs():The abs() method returns the absolute value of x i.e. the positive distance between x and zero e.g.

print(abs(-70))

Print(abs(10.66))

Result of above program will be 70 and 10.66.

max():The max() method returns the largest of its arguments i.e. the value closest to positive infinity e.g

Print(max(11,33,99,12,55))

Print(max(22,-33,23,991))

Result of above example will be 99 and 991.
min():The min() method returns the smallest of its arguments i.e. the value closest to negative infinity e.g

Print(max(11,33,99,12,55))

Print(max(22,-33,23,991))

Result of above example will be 11 and -33.

There are many functions that manipulate number variables.

Now let’s see some examples of string variable manipulation

capitalize():It returns a copy of the string with only its first character capitalized e.g.

str = "this is string example."

print (str.capitalize())

in above example result will be This is string example.

isalnum():The isalnum() method checks whether the string consists of alphanumeric characters e.g.

str = "this2016" # No space in this string

print (str.isalnum())

str = "this is string example."

print (str.isalnum())

Result

Ist print statement will print true as that statement contains alphanumeric character.

Second print statement will print false as it does not contain any alphanumeric character.

Islower():The islower() method checks whether all the case-based characters (letters) of the string are lowercase e.g.

str = "THIS is string example"

print (str.islower())

str = "this is string example”

print (str.islower())

Result ist statement will print false as there are upper case letters also.

2nd case will print true as there is no upper case letter.

Isupper():The isupper() method checks whether all the case-based characters (letters) of the string are uppercase e.g.

str = "THIS IS STRING EXAMPLE.”

print (str.isupper())

str = "THIS is string example”

print (str.isupper())

Result ist statement will print true as all the letters are upper case

2nd statement will print false as there are lower case letter in the statement.

L-value and R-value: let’s understand this concept with an example.

x=3+4

print(x)

In this example x is an L-value because it persists beyond the expression that defines it. The expression 3+4 is an R-value because it evaluates to a temporary value that does not persist beyond the expression that defines it.