0% found this document useful (0 votes)
10 views77 pages

Features of Python

The document provides an overview of Python fundamentals, including the character set, tokens, keywords, identifiers, literals, and data types. It explains the rules for creating identifiers, the types of literals supported by Python, and how to perform type conversion. Additionally, it covers input and output operations in Python, emphasizing that input values are treated as strings by default.

Uploaded by

saravagawal4321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views77 pages

Features of Python

The document provides an overview of Python fundamentals, including the character set, tokens, keywords, identifiers, literals, and data types. It explains the rules for creating identifiers, the types of literals supported by Python, and how to perform type conversion. Additionally, it covers input and output operations in Python, emphasizing that input values are treated as strings by default.

Uploaded by

saravagawal4321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 77
for more updates visit: www.python4csip.com PYTHON FUNDAMENTALS for more updates visit: www.python4csip.com Python Character Set Is a set of valid characters that python can recognize. A character represent letters, digits or any symbol. Python support UNICODE encoding standard. Following are the Python character set > Letters : A-Z, a-z > Digits :0-9 » Special symbols :space +-*/()~ !@#$%" & [{ e".<.>/2 White spaces : Blank space, Enter, Tab Other character : python can process all ASCII and UNICODE as a part of data or literal Vv v VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com TOKENS TT In a passage of text, individual words and punctuation marks are called tokens or lexical units or lexical elements. The smallest individual unit in a program is known as Tokens. Python has following tokens: vy Keywords v_Identifiers(Name) vy Literals v Operators v Punctuators VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com KEYWORDS EE Keywords are the reserved words and have special meaning for python interpreter. Every keyword is assigned specific work and it can be used only for that purpose. A partial list of keywords in Python is and del from not while as elif global or with assert else if pass yield break except import —_print class exec in raise continue finally is return def for lambda try for more updates visit: www.python4csip.com IDENTIFIERS kk Are the names given to different parts of program like variables, objects, classes, functions etc. Identifier forming rules of Python are : > \s an arbitrarily long sequence of letters and digits > The first character must be letter or underscore > Upper and lower case are different > The digits 0-9 are allowed except for first character > It must not be a keyword > No special characters are allowed other than underscore is allowed. » Space not allowed VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Grade-Pay Perl) Ea BS ee nd Cale LCS VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Literals / Values 0 Literals are data items that have a fixed value. Python supports several kinds of literals: © String Literal © Numeric Literals © Boolean Literals © Special Literals — None © Literal Collections VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com String Literals ae o a It is a collection of character(s) enclosed in a double or single quotes Examples of String literals “Python” “Mogambo” ‘123456' ‘Hello How are your’ 5 ‘$’, ‘4’,"@@" In Python both single character or multiple characters enclosed in quotes such as “kv”, ‘kv’,"*’,”+” are treated as same VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Non-Graphic (Escape) characters > eee oO They are the special characters which cannot be type directly from keyboard like backspace, tabs, enter etc. When the characters are typed they perform some action. These are represented by escape characters. Escape characters are always begins from backslash(\) character. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com List of Escape characters EE Eee aaaenennaaaaa] aa Sequence | What it does ee Backslash. \r Carriage return \ Sool ates I a Double quotes \uxxxx Hexadecimal value(16 bit) \a ASC bell \Uxxxx Hexadecimal value(32 bit) Back Space \ vertical tab LS -. Oa VINOD KUMAR VERMA, PGT(CS), KY OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com String type in Python EE ee aeaeaaaeal a Python allows you to have two string types: O Single Line Strings = The string we create using single or double quotes are normally single-line string i.e. they must terminate in one line. ™ For e.g if you type as = Name="KV and press enter = Python we show you an error “EOL while scanning string literal” = The reason is quite clear, Python by default creates single-line string with both single quotes and it must terminate in the same line by quotes VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com String type in Python |_| SRERSSEESSEESSRESSEESSURSSERESERESERESERESERESERESSERSSRESSY 0 Multiline String © Some times we need to store some text across multiple lines. For that Python offers multiline string. 5 To store multiline string Python provides two ways: (a) By adding a backslash at the end of normal Single / Double quoted string. For e.g. "1/6 Mall RoadKanpur' >>> VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com String type in Python EE ee aeaeaaaeal 2 Multiline String (b) By typing text in triple quotation marks for e.g. >>> Address=' New Delhi /7 Preet Vihar >>> print(Address) 1/7 Preet Vihar New Delhi India >>> Address ‘1/7 Preet Vihar\nNew Delhi\nIndia' VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Size of String 2 ) Python determines the size of string as the count of characters in the string. For example size of string “xyz” is 3 and of “welcome” is 7. But if your string literal has an escape sequence contained in it then make sure to count the escape sequence as one character. For e.g. WY 1 ‘abe’ 3 \ab! z “Meera\’s Toy” VW “Vicky's” x a You can check these size using len() function of Python. For example | >>>len(‘abc’) and press enter, it will show the size as 3 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Size of String 22 a For multiline strings created with triple quotes : while calculating size the EOL character as the end of line is also counted in the size. For example, if you have created String Address as: >>> Address="""Civil lines Kanpur" >>> len(Address) 18 © For multiline string created with single/double quotes the EOL is not counted. >>> data="ab\ be\ cd" >>> len(data) 6 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Numeric Literals EE ee aeaeaaaeal O The numeric literals in Python can belong to any of the following numerical types: 1) Integer Literals: it contain at least one digit and must not contain decimal point. It may contain (+) or (-) sign. 5 Types of Integer Literals: a) Decimal : 1234, -50, +100 b) Octal : it starts from symbol Oo (zero followed by letter ‘o’) = For e.g. 0010 represent decimal 8 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Numeric Literals EE ee aeaeaaaeal >>> num = 0010 >>> print(num) It will print the value 8 c) Hexadecimal : it starts from Ox (zero followed by letter ‘x’) >>> num = OoF >>> print(num) it will print the value 15 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Numeric Literals Sl eee EanrEaEanal 0 2) Floating point Literals: also known as real literals. Real literals are numbers having fractional parts. It is represented in two forms Fractional Form or Exponent Form co Fractional Form: it is signed or unsigned with decimal point © For eg. 12.0, -15.86, 0.5, 10. (will represent 10.0) co Exponent Part: it consists of two parts “Mantissa” and “Exponent”. © For e.g. 10.5 can be represented as 0.105 x 102 = 0.105E02 where 0.108 is mantissa and 02 (after letter E) is exponent VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Points to remember A SE © Numeric values with commas are not considered int or float valve, rather Python treats them as tuple. Tuple in a python is a collection of values or sequence of values. (will be discussed later on) 2 You can check the type of literal using type() function. For e.g. >>> a=100 >>> type(a) >> b=10.5 >>> type(b) >>> name="hello* >>> type(name) >>> a=100,50,600 >>> type(a) VINOD KUMAR VERMA, PGT(CS), KV OFF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Boolean Literals EE ee aeaeaaaeal A Boolean literals in Python is used to represent one of the two Boolean values i.e. True or False These are the only two values supported for Boolean Literals For e.g. >>> isMarried=True >>> type(isMarried) VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Special Literals None SSS Python has one special literal, which is None. It indicate absence of value. In other languages it is knows as NULL. It is also used to indicate the end of lists in Python. >>> salary=None >>> type(salary) VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Complex Numbers Complex: Complex number in python is made up of two floating point values, one each for real and imaginary part. For accessing different parts of variable (object) x; we will use x.real and x.image. Imaginary part of the number is represented by ‘” instead of “I”, so 1+0j denotes zero imaginary. part. Example >>> x = 140} >>> print x.real,x.imag 1.0 0.0 Example >>> y = 9-5] >>> print y.real, y.imag 9.0 -5.0 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Conversion from one type to another a a Python allows converting value of one data type to another data type. If it is done by the programmer then it will be known as type conversion or type casting and if it is done by compiler automatically then it will be known as implicit type conversion. Example of Implicit type conversion >>> x= 100 >>> type(x) >>> y =12.5 >>> typely) >>> x=y >>> type(x) # Here x is automatically converted to float VINOD KUMAR VERMA, PGT(CS], KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Conversion from one type to another EE eee Explicit type conversion To perform explicit type conversion Python provide functions like int(), float(), str(), bool() >>> a=50.25 >>> b=int(a) >>> print b 50 Here 25 i: inverted to int val: >>>a=25 >>>y=float(a) >>>print y 25.0 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Simple Input and Output 2 a In python we can take input from user using the built-in function input(). co Syntax variable = input() Note: value taken by input() function will always be of String type, so by default you will not be able to perform any arithmetic operation on variable. >>> marks=input("Enter your marks ") Enter your marks 100 >>> type(marks) Here you can see even we are entering value 100 but it will be treated as string and will not allow any arithmetic operation VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Simple Input and Output EE ee aeaeaaaeal >>> salary=input("Enter your salary ") Enter your salary 5000 >>> bonus = salary*20/100 Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’ VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Reading / Input of Numbers Sl eee EanrEaEanal oO Now we are aware that input() function value will always be of string type, but what to do if we want number to be entered. The solution to this problem is to convert values of input() to numeric type using int() or float() function. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Possible chances of error while taking nput as numbers we ets MUO rs 1. Entering float value while converting to int >>> num1=int(input("Enter marks ")) Enter marks 100.5 Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: ‘100.5! 2. Entering of values in words rather than numeric >>> age=int(input("What is your age ")) What is your age Eighteen Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: Eighteen’ VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Possible chances of error while taking nput as numbers we ets MUO rs 3. While input for float value must be compatible. For e.g. Example 1 >>> percentage=float(input("Enter percentage ")) Enter percentage 1 2.5.6 Traceback (most recent call lost): File "", line 1, in ValueError: could not convert string to Hoat: '12.5.6" Example 2 >>> percentage=float(input("Enter percentage ")) Enter percentage 100 percent Traceback (most recent call last): File "", line 1, in ValueError: could not convert string to float: ‘100 percent’ VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Program 1 |_| SRERSSEESSEESSRESSEESSURSSERESERESERESERESERESERESSERSSRESSY Open a new script file and type the following code: num] =int(input("Enter Number 1 ")) num2=int(input("Enter Number 2 ")) num3 = num1 + num2 print("Result =",num3) Save and execute by F5 and observe the result VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Let us write few programs ca WAP to enter length and breadth and calculate area of rectangle 2 WAP to enter radius of circle and calculate area of circle WAP to enter Name, marks of 5 subject and calculate total & percentage of student 2 WAP to enter distance in feet and convert it into inches a WAP to enter value of temperature in Fahrenheit and convert it into Celsius. oc WAP to enter radius and height of cylinder and calculate volume of cylinder. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Operators SSS Oo are symbol that perform specific operation when applied on variables. Take a look at the expression: (Operator) 10+ 25 (Operands) Above statemenHis-an expression (combination of operator and operands) i.e. operator operates on operand. some operator requires two operand and some requires only one operand to operate VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Types of Operators a Unary operators: are those operators that require one operand to operate upon. Following are some unary operators: + Unary plus a Unary minus as Bitwise complement Not Logical negation VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Types of Operators seen ee a SESE EET] © Binary Operators: are those operators that require two operand to operate upon. Following are some Binary operators: 1. Arithmetic Operators a + Addition = Subtraction a Multiplication / Division % Remainder ss Exponent // Floor division VINOD KUMAR VERMA, PGT(CS), KY OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Example SSS >>> numl=20 >>> num2=7 >>> val = num] % num2 >>> print{val) 6 >>> val = 2**4 >>> print(val) 16 >>> val = num! / num2 >>> print(val) 2.857 142857142857 >>> val = num1 // num2 >>> print(val) 2 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Bitwise operator io Ce cmd & Bitwise AND Return 1 if both 4 k Cee aC eg inputs are 1 Cao mCattrel| a Bitwise XOR Return 1, if the Sree a a) mimbes of 0 fa Pee er OY inpunieintodd operator it will work on SCO Meee sad Bitwise OR Return 1 if any Cnet input is 1 result in decimal not in Cia VINOD KUMAR VER} SACHIN BHARDWAJ, PGT for more updates visit: www.python4csip.com Example =o >>> print(12 & 7) Binary of 12 is 1100 and 7 is 0111, so applying & 4 a 1100 oll Guess the output with | and 42 0100 Which is equal to decimal value 4 Let us see one practical example, To check whether entered number is a power of 2 or not) like 2, 4, 8, 16, 32 and so on To check this, no need of loop, simply find the bitwise & of n and n-1, if the result is O it wer of 2 otherwise not isible of 2 (or in int (input ("Enter any number “)) 7 Enter any number 32) Enter any number 24 fr =n &n-l >, 16 int (r) Later on by using ‘if’ we can print Hshdeaend me ba a wer nee meaningful message memenete \not in power of 2 VINOD KUMAR VERMA, PGT(CS), KV OEF KAN SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Shift Operators a Shift left >> Shift right is Is the Identity same? is not Is the identity not same? VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Relational Operators | _ /SesEESESUSEERSuESESERSERSEASNSUEEESSESESEESSESNESERSESEESNGS| Less than > Greater than <= Less than or Equal to >= Greater than or Equal to — Equal to != Not equal to VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Logical Operators eens and Logical AND or Logical OR not Logical NOT VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGTICS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com oo Operators Assignment /= Assign quotient += Assign sum -= Assign difference oS Assign product we Assign Exponent //= Assign Floor division VINOD KUMAR VERMA, PGT(CS), KY OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Membership Operators eens in Whether variable in sequence not in Whether variable not in sequence VINOD KUMAR VERMA, PGT(CS), KY OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Punctuators See eee co Punctuators are symbols that are used in programming languages to organize sentence structure, and indicate the rhythm and emphasis of expressions, statements, and program structure. oc Common punctuators are: ‘ “ # $ @ []{}=::(),- VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Barebones of Python Program > Itmeans basic structure of a Python program Ma Eiiraietallccl'ct fellewingleoder #This program shows a program's component # Definition of function SeeYou() follows def SeeYou(): print("“This is my function”) #Main program ~ Comments Function A=10 B=A+20 z _ Expressions 5 inl t | if(C>=100) #checking condition ito Cai 3 prini(""Valve is equals or more than 100") 5 else: ie ~ print(""Value is less than 100”) Block 2 | seeYou) #Calling Function = 3 VINOD KUNAR VERMA, PGT(CS) KV OFF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Expression Sl eee EanrEaEanal co An expression is any legal combination of symbols that represents a value. An expression is generally a combination of operators and operands Example: expression of values only 20, 3.14 Expression that produce a value when evaluated At+10 Salary * 10 / 100 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Statement See eee oO It is a programming instruction that does something i.e. some action takes place. Oo Example print(““Welcome to python”) The above statement call print function When an expression is evaluated a statement is executed i.e. some action takes place. a=100 b=b+20 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Comments EE ee aeaeaaaeal a Comments are additional information written in a program which is not executed by interpreter i.e. ignored by Interpreter. Comment contains information regarding statements used, program flow, etc. 3 Comments in Python begins from # co Python supports 3 ways to enter comments: ). Full line comment 2. Inline comment 3. Multiline comment VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Comments A © Full line comment Example: #This is program of volume of cylinder a Inline comment Example area = length*breadth # calculating area of rectangle a Multiline comment Example 1 (using #) # Program name: area of circle # Date: 20/07/18 #Language : Python VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Comments Sl eee EanrEaEanal co Multiline comment (using “ “ “‘) triple quotes Example Program name : swapping of two number Date : 20/07/18 Logic : by using third variable 99909 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Functions EE eee] 3 Function is a block of code that has name and can be reused by specifying its name in the program where needed. It is created with def keyword. Example def drawline(): print(“==' print(“Welcome to Python’) drawline() print(“Designed by Class XI’) drawline() VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Block and Indentation EE ae a Group of statement is known as block like function, conditions or loop etc. Oo For e.g. def area(): a=10 b=5 c=a*b Indentation means extra space before writing any statement. Generally four space together marks the next indent level. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Variables EE ee aeaeaaaeal 2 Variables are named temporary location used to store values which can be further used in calculations, printing result etc. Every variable must have its own Identity, type and value. Variable in python is created by simply assigning value of desired type to them. Oo For eg co Num = 100 a Name=“James” VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Variables Sl eee EanrEaEanal 2 Note: Python variables are not storage containers like other programming language. Let us analyze by example. co In C++, if we declare a variable radius: radius = 100 [suppose memory address is 41260] Now we again assign new value to radius radius = 500 Now the memory address will be still same only value will change VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Variables EE eee 3 Now let us take example of Python: radius = 100 [memory address 3568] radius = 700 [memory address 8546] Now you can see that In python, each time you assign new value to variable it will not use the same memory address and new memory will be assigned fo variable. In python the location they refer to changes every time their value change.(This rule is not for all types of variables) VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com Lvalues and Rvalues CH a Lvalue : expression that comes on the Left hand Side of Assignment. a Rvalue : expression that comes on the Right hand Side of Assignment Lvalue refers to object to which you can assign value. It refers to memory location. It can appear LHS or RHS of assignment Rvalue refers to the value we assign to any variable. It can appear on RHS of assignment VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & ‘SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

You might also like