Download as pdf
Download as pdf
You are on page 1of 4
Ge ryvCBSEguidecom 4 Compete gid for CHSE stndets CBSE Revision Notes Class-11 Computer Science (New Syllabus) Unit 1: Programming and Computational Thinking (PCT-1) Variables and Manipulating Methods Variable is aname assigned to a memory location to store the data. The data that a variable can store depends on data type for example Here x,y and z are name of three variables all of them are holding different data depending on their data type eg 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 itis 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. def fund: s=10 print(s) return ee print(s) Material downloaded from myCBSEguide.com. a Ge ryvCBSEguidecom 4 Compete gid for CHSE stndets 6. fund) At line no 1 we have defined a function and have declared variable inside that namely s function ends at line no 4. Atline 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 fund: 3. print(s) 4. return 5. print(s) 6. fund At line no 1 sis 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)) Material downloaded from myCBSEguide.com. aia Ge ryvCBSEguidecom 4 Compete gid for CHSE stndets Result of above example will be 99 and 991. minO:The minOQ method returns the smallest of its arguments ie. 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.capitalizeQ) in above example result will be This is string example. isalnum(0:The isamum( method checks whether the string consists of alphanumeric characters e.g. str = "this2016" # No space in this string print (str.isalnum0) 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. Material downloaded from myCBSEguide.com. aia Ge ryvCBSEguidecom 4 Compete gid for CHSE stndets str = "THIS is string example" print (str.islower0) str = "this is string example” print (str.islowerQ) Result ist statement will print false as there are upper case letters also. 2d case will print true as there is no upper case letter. Isupper0: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.isupperQ) str HIS is string example” print (str.isupper() Result ist statement will print true as all the letters are upper case 2d 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. Material downloaded from myCBSEguide.com. aia

You might also like