PYTHON - NOTES
CLASS 10 – ARTIFICIAL INTELLIGENCE
❖ Python was created by Guido Van Rossum
❖ The language was released in February I991
❖ Expressive language
❖ Interpreted Language
❖ Its completeness
❖ Cross-platform Language
❖ Fee and Open source
❖ Variety of Usage / Applications
In a passage of text, individual words and punctuation marks
are called tokens or lexical units or lexical elements. Thesmallest
individual unit in a program is known as Tokens.
Python has following tokens:
Literals
✓ Keywords # Simple Python Program
✓ Identifiers(Name) for i in range(1,10):
if i %2 != 0: Punctuators
✓ Literals print(i)
✓ Operators Keywords Operators
✓ Punctuators
Identifier
Are the names given to different parts of program like variables,
objects, classes, functions etc.
Identifier forming rules of Python are :
➢ Is 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
GradePay File_12_2018 JAMES007
GRADEPAY _ismarried _to_update
Grade-Pay 12_2018_File $JAMES007
if RollNo. Roll No
• Literals are data items that have a fixed value. Python
supports several kinds of literals:
•String Literal
•Numeric Literals
•Boolean Literals
String Literals:
It is a collection of character(s) enclosed in a double or single
quotes. It can be either Single line strings or Multiline Strings
SINGLE LINE STRING : must terminate in one line i.e. the closing
quotes should be on the same line as that of the opening
quotes
Examples of String literals
“Python”
“Mogambo”
„123456‟
„Hello How are your‟
„$‟, „4‟,”@@”
MULTI LINE STRING : To store multiline string Python provides
two ways:
(1) By adding a backslash at the end of normal Single / Double
quoted
(2) By typing text in triple quotation marks
string. For e.g.
for e.g.
>>> Name="1/6 Mall Road \ Kanpur"
>>>Address="""1/7 Preet Vihar
>>> Name
New Delhi
'1/6 Mall RoadKanpur'
India"""
• Punctuators are symbols that are used in programming languages to
organize sentencestructure, and indicate the rhythm and emphasis of
expressions, statements, and program structure.
• Common punctuators are: „“ # $ @ []{}=:;(),.
• In python we can take input from user using the built-
in function input().
• Syntax
variable = input(<message to display>)
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)
<class 'str'>
Hereyoucanseeevenwe are enteringvalue100 butit will be treated as
string and will not allow any arithmetic operation.
• 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. Thesolutionto this problemis to convert valuesof input() to
numerictypeusingint()or float() function.
>>> age = int(input(“Enter your age “))
>>>print type(age)
<type “Iint‟>
>>>salary = float(input(“Enter salary “))
>>>print type(salary)
<type “float‟>
• Python allows to display output using print().
Example 1
print(“Welcome”)
Example 2
print(100)
Example 3
Age=20
print(“Your age is “, Age)
Control Statements:
range() function
▪ For e.g.
▪ range(1,10) will generate set of values from
1-9
▪ To create list from ZERO(0) we can use range(10) it will generate
▪ range(0,7) will generate [0-6]
[0,1,2,3,4,5,6,7,8,9]
▪ Default step value will be +1 i.e.
▪ range(1,10) means (1,2,3,4,5,6,7,8,9) range(0,10,2)
[0,2,4,6,8]
▪ To change the step value we can
use third parameter in range() which is step value range(10,2,-3)
[10,7,4]
▪ For e.g.
▪ range(1,10,2)
[1,3,5,7,9]
▪ Step value can be in –ve also to generate set of numbers in reverse
order.
▪ range(10,0) will generate number as [10,9,8,7,6,5,4,3,2,1]