Thursday, May 9, 2019

PYTHON CLASS XI


CHAPTER 1
PYTHON
Interactive, interpreted, and object-oriented programming language.
• Simple syntax
• Developed by Guido Van Rossum in 1991 at the National Research Institute for Mathematics and Computer Science in the Netherlands.
• Name was inspired by: Monty Python’s Flying Circus
1.1 PYTHON PROGRAMMING ENVIRONMENT
Available on a wide variety of platforms including Windows, Linux and Mac OS X.
• Official Website: python.org
• IDLE stands for Integrated Development and Learning Environment. Python IDLE com- prises Python Shell and Python Editor. Python Shell Python Editor
1.2 Display on screen
>>>print('hello world')
hello world
1.3 Names (Variables) and Assignment Statements
• Variables provide a means to name values so that they can be used and manipulated later.
• Assignment Statement: Statement that assigns value to a variable.
>>> english = 57
>>>print(english)
Python associates the name (variable) english with value 57 i.e. the name (variable) english is assigned the value 57, or that the name (variable) english refers to value 57. Values are also called objects.
1.3.1 Rules for creating a name (variable)
• Must begin with a letter or _ (underscore character)
• May contain any number of letters, digits, or underscore characters. No other character apart from these is allowed.

1.3.2 Shorthand Notation
In [3]: a = 6
a = a + 5
print(a)
 a = 6
a += 5
print(a)
11 11
2

1.3.3 Multiple Assignments
• Used to enhance the readability of the program.
>>>msg, day, time = 'Meeting', 'Mon', '9'
totalMarks = count = 0
1.4 Arithmetic Operators
>>>print("18 + 5 =", 18 + 5) #Addition
print("18 - 5 =", 18 - 5) #Subtraction
print("18 * 5 =", 18 * 5) #Multiplication
print("27 / 5 =", 27 / 5) #Division
print("27 // 5 =", 27 // 5) #Integer Division
print("27 % 5 =", 27 % 5) #Modulus
print("2 ** 3 =", 2 ** 3) #Exponentiation
print("-2 ** 3 =", -2 ** 3) #Exponentiation
18 + 5 = 23
18 - 5 = 13
18 * 5 = 90
27 / 5 = 5.4
27 // 5 = 5
27 % 5 = 2
2 ** 3 = 8
-2 ** 3 = -8
>>>print("'how' + ' are' + ' you?':", 'how' + ' are' + ' you?')
print("'hello' * 5 :", 'hello' * 5)
'how' + ' are' + ' you?': how are you? 'hello' * 5 : hellohellohellohellohello

Precedence of Arithmetic Operators
1.5 Relational Operators
• Used for comparing two expressions and yield True or False.
• The arithmetic operators have higher precedence than the relational operators.
>>> print("23 < 25 :", 23 < 25) #less than
print("23 > 25 :", 23 > 25) #greater than
 print("23 <= 23 :", 23 <= 23) #less than or equal to
print("23 - 2.5 >= 5 * 4 :", 23 - 2.5 >= 5 * 4) #greater than or equal to
print("23 == 25 :", 23 == 25) #equal to
print("23 != 25 :", 23 != 25) #not equal to
• When the relational operators are applied to strings, strings are compared left to right, character by character, based on their ASCII codes, also called ASCII values.
>>>print("'hello' < 'Hello' :", 'hello' < 'Hello')
>>>print("'hi' > 'hello' :", 'hi' > 'hello')
'hello' < 'Hello' : False 'hi' > 'hello' : True’
1.6 Logical Operators
• The logical operators not, and, and or are applied to logical operands True and False, also called Boolean values, and yield either True or False.
• As compared to relational and arithmetic operators, logical operators have the least precedence level.
>>>print("not True < 25 :", not True) #not operator
>>>print("10 < 25 and 5 > 6 :", 10 < 25 and 5 > 6) #and operator
>>> print("10 < 25 or 5 > 6 :", 10 < 25 or 5 > 6) #or operator
Precedence of Logical Operators
Python Keywords
• Reserved words that are already defined by the Python for specific uses.
>>>import keyword
print(keyword.kwlist)
[’False’, ’None’, ’True’, ’and’, ’as’, ’assert’, ’break’, ’class’,’continue’, ’def’, ’del’, ’elif’, ’else’, ’except’, ’finally’, ’for’, ’from’, ’global’, ’if’, ’import’, ’in’, ’is’, ’lambda’,’nonlocal’, ’not’, ’or’, ’pass’, ’raise’, ’return’, ’try’, ’while’, ’with’, ’yield’]