What Is Python ?
Total Page:16
File Type:pdf, Size:1020Kb
Class-XI Computer Science Python Fundamentals Notes What is python ? Python is object oriented high level computer language developed by Guido Van Rossum in 1990s . It was named after a British TV show namely ‘Monty Python’s Flying circus’. Python is a programming language, as are C, Fortran, BASIC, PHP, etc. Some specific features of Python are as follows: • an interpreted (as opposed to compiled) language. Contrary to e.g. C or Fortran, one does not compile Python code before executing it. In addition, Python can be used interactively: many Python interpreters are available, from which commands and scripts can be executed. • a free software released under an open-source license: Python can be used and distributed free of charge, even for building commercial software. • multi-platform: Python is available for all major operating systems, Windows, Linux/Unix, MacOS X, most likely your mobile phone OS, etc. • a very readable language with clear non-verbose syntax • a language for which a large variety of high-quality packages are available for various applications, from web frameworks to scientific computing. • a language very easy to interface with other languages, in particular C and C++. • Some other features of the language are illustrated just below. For example, Python is an object-oriented language, with dynamic typing (the same variable can contain objects of different types during the course of a program). Python character set: • Letters : A-Z , a-z • Digits : 0-9 • Special symbols : space,+,-,*,/,** etc. • Whitespace : blank space, tabs(->),carriage return, newline, formfeed • Other characters :python can process all ASCII and unicode characters as part of data PYTHON TERMINOLOGY : Tokens: A smallest individual unit in a program is known as tokens. For Example: He said, “Hurry up!”. Here in this English sentence two types of tokens are used : word and punctuators. In Program individual units or tokens together make components of a program. For Example : # A sample program name = “Ram” age = 18 marks = 99.5 print(“NAME : ”, name, “AGE :”, age, “MARKS :” ,marks) Here many tokens are present such as keyword, identifier and literals. Keyword : print( ) String literal : “Ram” Integer literal : 18 Floating point literal :99.5 Punctuator : “ ”, : , Identifier : name , age , marks Python has following tokens: a) Keywords b) Identifiers c) Literals d) Operators e) Punctuators Keywords: It is a word having special meaning and must not be used as normal identifier. Ex : if , while ,int etc. Identifier: They are fundamental building blocks of a program and used to declare variable, objects, class, function etc. Identifier’s forming rules are: 1) An identifier is an arbitrarily long sequence of letters and digits. 2) First character must be letter. 3) No special character except (_) is allowed. 4) Must not be keyword. 5) Upper and lower case letters are different. 6)Identifiers are unlimited in length. 7)The digits 0 to 9 can be the part of the identifier except the first character. Some valid identifiers are : Myfile, chk chk12, first_name file2 Some invalid identifiers are: Data-rec 29digits break my.file first$name Literals: They are data items that have a fixed value. Different types of literals are: 1) Strings 2) Numeric 3) Boolean 4) Special literal- none 5) Literal collection 1) String: String literals are enclosed in quotes(either in single or double) . For ex: ‘a’,’abc’,”x”,”xyz” It also allows to have certain nongraphic characters (those character that cannot be typed directly from keyboard e.g. backspace, tabs, carriage return etc. ) . There are two string types: (A) single line (B) multiline Single line string: are created by enclosing text in singe quotes (‘ ’) or double quotes (“ ”) and must terminate in one line. Name= “ram” Name1= ‘sita’ Grade = ‘a’ Multiline string: It can be created in two ways: (i) By adding a backslash Ex: text1 = ‘hello\ World’ (ii) By adding triple quotation mark Ex: str1 = ‘‘‘hello World. There I come!!! Cheers’’’ (You can use double quotes also) Size Of String Python determines the size of the string as the count of characters in the string.Consider some of the example given below: “abc” size is 3 “\ab” size is 2( \a is an escape sequence, thus one character ) ‘\\’ size is 1(\\ is an escape sequence to represent backslash) “Seema\’s pen” size is 11(count the characters including blank character) 2) Numeric: It is of three types int, float and complex . Integer: literals are whole numbers without any fractional part. There are three types of integer literals. (a) Decimal integer literals: A sequence of digit unless it begins with 0 is taken to be a decimal integer literal. Ex: 23 ,456 etc. (b) Octal integer literals: A sequence of digit (only from 0-7, 8 & 9 are invalid) staring with 0 is octal integer. Ex :012,0345 (c) Hexadecimal integer literals: A sequence of digit (from 0-9 & 10-A, 11-B, 12- C, 13-D,14-E, 15-F) preceded by ox or OX is taken to be a hexadecimal number. Ex: 0xAB,0X1,0XCD3 etc. Floating point literals: They are also called real literals. They are numbers having fractional part. These may be written in one of the two forms called fractional form and exponent form (a) Fractional form: It consists of signed or unsigned digits including a decimal point between digits for ex: 2.0, -13.0, 0.7. The rule for writing a real literal in fractional form is: A real constant in fractional form must have at least one digit with the decimal point, either before or after, It may also have either + or - sign preceding it. A real constant with no sign is assumed to be positive. (b) Exponent form: A real constant in Exponent format consists two parts: mantissa and exponent. Ex: 5.8 will be written as 0.58*10 = 0.58E01 Where 0.58 is mantissa and 1 is exponent. Some invalid real literals in exponent form: 1.7 (no digit specified for exponent) 0.17E2.3 (Exponent cannot have fractional part) The rule for writing a real literal in exponent form is : A real constant in exponent form has two parts: a mantissa and an exponent . The mantissa must be either an integer or a proper real constant. The mantissa is followed by a letter E or e and an exponent. The exponent must be an integer. Complex numeric literal : a +b J or a+ b j a is real part and b J is imaginary part. 3) Boolean literal : True and False are the only two Boolean literal values in Python. 4)special literal None : It indicates absence of value . Value1 =10 Value2 = None print(value1) print(value2) 10 None 5) Literal Collections : List , Tuple, Dictionary and sets are called literal collection . Delimiters: It is a sequence of one or more characters used to specify the boundary between separate independent regions in plain text or other data streams. Most common delimiters are () , {} , [] , @ , # , : , = etc. Operators: They are the tokens that trigger some computations when applied to variables. There can be unary or binary. Unary operator requires one operand whereas binary operators require two operand. For ex: +a (unary operator +) a + b (binary operator +) Unary Operators : Unary operators are those operators that require one operand to operate upon. Following are some unary operators: + Unary plus - Unary minus ~ Bitwise complement not logical negation Different types of Binary operator are: ➢ Relational operator (< , > , >= , <= , == , !=) ➢ Logical operator (and , or ) ➢ Assignment operator(/= , += , *= , -=) ➢ Membership operator( in , not in ) ➢ Arithmetic operators(+,-,*,/,//,%) ➢ Identity operators (is, is not) The operations are represented by operators and the objects of the operation are referred to as operands. An operator is used to perform specific mathematical or logical operation on values. The values that the operators work on are called operands. For example, in the expression 10 + num, the value 10, and the variable num are operands and the + (plus) sign is an operator. Python supports several kinds of operators whose categorization is briefly explained in this section. Python compares strings lexicographically, using ASCII value of the characters. If the first character of both the strings are same, the second character is compared, and so on. ARITHEMETIC OPERATOR: Python supports arithmetic operators that are used to perform the four basic arithmetic operations as well as modular division, floor division and exponentiation. + Addition adds the two numeric values on either side of the operator. This operator can also be used to concatenate two strings on either side of the operator >>> num1 = 5 >>> num2 = 6 >>> num1 + num2 11 >>> str1 = "Hello" >>> str2 = "India" >>> str1 + str2 'HelloIndia' - Subtraction Subtracts the operand on the right from the operand on the left >>> num1 = 5 >>> num2 = 6 >>> num1 - num2 -1 * Multiplication Multiplies the two values on both side of the operator >>> num1 = 5 >>> num2 = 6 >>> num1 * num2 30 It Repeats the item on left of the operator if first operand is a string and second operand is an integer value. >>> str1 = 'India' >>> str1 * 2 'IndiaIndia' / Division Divides the operand on the left by the operand on the right and returns the quotient. >>> num1 = 8 >>> num2 = 4 >>> num2 / num1 0.5 % Modulus Divides the operand on the left by the operand on the right and returns the remainder. >>> num1 = 13 >>> num2 = 5 >>> num1 % num2 3 // Floor Division Divides the operand on the left by the operand on the right and returns the quotient by removing the decimal part. It is sometimes also called integer division. >>> num1 = 13 >>> num2 = 4 >>> num1 // num2 3 >>> num2 // num1 0 ** Exponent Performs exponential (power) calculation on operands. That is, raise the operand on the left to the power of the operand on the right >>> num1 = 3 >>> num2 = 4 >>> num1 ** num2 81 RELATIONAL OPERATOR: Relational operator compares the values of the operands on its either side and determines the relationship among them.