Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Class-IX-Python-Basics

Uploaded by

namitjain291
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Class-IX-Python-Basics

Uploaded by

namitjain291
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Why he named

PYTHON???
He was a big fan
of Britain
Comedy movie-
Monty Python’s
Flying Circus
1.4 Working with Python
Before we start writing a program, we should be thoroughly clear in our mind about
the steps of solving the problem. If we start writing the program without knowing
how to solve the problem, then we can never write the correct program.
This leads to the concept of Computational Thinking.

Extension of Python file: .py [script mode]; .pyw [Interactive mode-Shell]


2.3 Tokens
A token is a smallest element of a Python script that is meaningful to the interpreter. Python
has following tokens:
 Keywords
 Identifiers [Names]
 Literals [Values]
 Operators
 Delimiters [Punctuators]
 Keywords:
Keywords are the words that convey a special meaning to the language compiler/ interpreter.
These are reserved for special purpose and must not be used as normal identifier names.
Example: True, break, in, class, print, def, del, etc.
 Identifiers [Names]:
Identifiers are fundamental building blocks of a program and are used as the general
terminology for the names given to different parts of the programs- variables, functions, lists,
modules, etc.
Identifier forming RULES:
 Sequence of letters and digits
 First character must be a letter. Underscore ( _ ) is also a letter.
 Uppercase and lowercase letters are different. (Case sensitive)
 Digits 0-9 can be a part of the identifier except for the first character.
 Identifiers are unlimited in length.
 Identifier must not be a keyword of Python
 Identifier can not contain any special character except ( _ ).
 Literals [Values]:
Literals (often referred to as constant-Values) are data items that have a fixed value.
Python allows several kinds of literals:
*String Literals *Numeric Literals *Boolean Literals *Special Literal-None

String Literal: a sequence of characters surrounded by quotes( single or double or


triple quotes)

Python allows certain nongraphic characters in string values. Nongraphic characters


are those characters that cannot be typed directly from keyboard e.g., backspace,
tabs, carriage return etc. These nongraphic characters can be represented by using
escape sequences. An escape sequence is represented by a backslash( \ ) followed
by one or more characters.

Commonly used Escape Sequences in Python:


\\ : Backslash (\)
\’ : Single quote (‘)
\” : Double quote (“)
\n : New line character
\t : Horizontal Tab
Types of String in python:
 Single line strings: surrounded by quotes( single or double or triple
quotes). They must terminate in one line.
Try:
s=‘hello #(press enter to move to the next line)
world’
 Multiline strings: to store text spread across multiple lines as one
single string.
By typing the text in triple quotation marks.
Example: s=‘’’Welcome Example: s=‘’’Welcome
to the world to the world
of Programming’’’ of Programming’’’
Numeric Literals:
int : integers, positive or negative whole numbers
float : float represents real numbers with a decimal point

Boolean Literals:

Used to represent one of the two Boolean values (True or False).

Special Literal: None

None is used to indicate absence of value. Also means “there is no useful information” or
“there is nothing here at present”.
OPERATORS in PYTHON
Operators are tokens that trigger some computation when applied to variables and other
objects in an expression.
Variables and objects to which the computation is applied, are called operands.
An operator requires some operands to work upon.

Unary Operator: that require one operand to operate upon.


Unary Operators:
+ Unary Plus
- Unary Minus
~ Bitwise complement
Not logical negation
Binary Operators: that require two operands to operate upon.

Arithmetic Operators: +, - , * , / , // , % , ** .

Relational Operators: <, >, <=, >=, ==, !=

Logical Operators: and, or, not

Assignment Operators: =, +=,-=, *=, /=, //=, %=, **=

Membership operators: in, not in

Identity Operators: is , is not


PRECEDENCE OF OPERATORS in PYTHON
The following table lists all operators from highest precedence to lowest.
Operator precedence affects how an expression is evaluated.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first multiplies 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom.
Operator Description
** Exponentiation (raise to
the power) Example
~+- Complement, unary plus
# Python
and minus (method a = 20 b = 10 c = 15 d = 5 e = 0
names for the last two are e = (a + b) * c / d #( 30 * 15 ) / 5
+@ and -@) print "Value of (a + b) * c / d is ",e
* / % // Multiply, divide, modulo e = ((a + b) * c) / d # (30 * 15 ) / 5
and floor division
print "Value of ((a + b) * c) / d is ", e
+- Addition and subtraction e = (a + b) * (c / d); # (30) * (15/5)
>> << Right and left bitwise shift print "Value of (a + b) * (c / d) is ",e
& Bitwise 'AND'td> e = a + (b * c) / d; # 20 + (150/5)
^| Bitwise exclusive `OR' and print "Value of a + (b * c) / d is ", e
regular `OR' When you execute the above program,
<= < > >= Comparison operators it produces the following result −
<> == != Equality operators Value of (a + b) * c / d is 90
= %= /= //= -= += *= **= Assignment operators Value of ((a + b) * c) / d is 90
is is not Identity operators
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50
in not in Membership operators
not or and Logical operators
COMMENTS in PYTHON

Comments are the additional readable information, which is read by the programmers but
ignored by Python Interpreter. Comments begin with Symbol (#).

For example:
# This is a line of comment in our program

if b<5: #colon means it requires a block

Multi Line Comments:


First Method:

# Line 1 of comment
# Line 2 of comment
# Line 3 of comment

Second Method:
‘’’
Text in
Multi lines end with
‘’’
Various Components of a Python program:
Practical: 1
>>> num=25
>>> a=10
>>> num+a
35
>>> var=green
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
var=green
NameError: name 'green' is not defined
>>> var="green"
>>> var
'green'
>>> print(var)
green
>>> print("my favourite colour is " var)
SyntaxError: invalid syntax
>>> print ("my favourite colour is ",var)
my favourite colour is green
>>> print ("my favourite colour is " +var)
my favourite colour is green
Practical: 2
>>> 2+3
5
>>> 5-2
3
>>> 3*2
6
>>> 4/2
2.0
>>> 4//2
2
>>> 5/2
2.5
>>> 5//2
2
>>> 2**2
4
>>> 2**3
8
>>> 4%2
0
>>> 5%2
1
>>> print ("remainder is: ", 5%2)
remainder is: 1
# Empty statement:
age=20
if age>=20:
print(“Eligible”)
else:
pass

1. Simple Statements

2. Compound Statements
4.3 Flow Control Statements:
Revision Assignment
1. Write the extension of python files.
2. Write the different modes in which we can work with python.
3. State the difference between Keyword and Identifier.
4. What are operators? Explain different types of operators.
5. Evaluate: 56/3+6//2*21 [Use operator precedence chart]
6. How many types of strings are supported in Python?
7. What is None literal in Python?
8. What are data types? List Python’s built-in core data types.
9. What is the role of Comments and Indentation in a program.
10. What is a Statement? What is the significance of an empty
statement.
11. How Algorithm is different from a flowchart?
12. Define Variable. Can we use (.) in variable name? [Refer identifier
forming rules]
13. Name the conditional statement in python. Explain its use with an
example.
Practical Assignment
1. Write a python code to input two numbers, find and display their
sum.
2. Write a python code to calculate the radius if diameter is given.
3. Write the python code to calculate the Simple Interest.
4. Input two numbers and swap (interchange) the values.
5. Write a python code to calculate the area of circle and rectangle.
6. Write a python code to calculate the area of circle and rectangle.
[Menu based]
7. Write the python code to input two numbers and print the greatest
one.
8. Write the python code to input a number and display “EVEN” if it is
even otherwise display “ODD” if it is odd.
9. Write a program to input two numbers, find their sum and check if
sum is odd print its cube else print its square.
Practical Test
1. Write a python code to input two numbers, find and display their
sum.
2. Write a python code to calculate the radius if diameter is given.
3. Input age from the user and print if the person is eligible to vote or
not.
4. Write a python code to calculate the area of a rectangle.
5. Write the python code to input two numbers and print the greatest
one.
6. Write a program to input two numbers, find their sum and check if
sum is odd or even.

You might also like