0% found this document useful (0 votes)
390 views8 pages

Namma Kalvi 12th Computer Science Chapter 5 and 6 Notes em 214847

This document discusses Python variables and operators. It covers: 1. The key features of Python including its general purpose, platform independence, readability, and use for both scientific and non-scientific programming. 2. The different modes for testing Python programs - interactive mode and script mode. Interactive mode allows writing code directly in the Python prompt while script mode uses separate .py files. 3. Input and output functions like print() and input() with examples of displaying output and accepting user input. 4. Tokens as the basic building blocks of Python programs including identifiers, keywords, operators, delimiters and literals. Identifiers must start with a letter or underscore.

Uploaded by

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

Namma Kalvi 12th Computer Science Chapter 5 and 6 Notes em 214847

This document discusses Python variables and operators. It covers: 1. The key features of Python including its general purpose, platform independence, readability, and use for both scientific and non-scientific programming. 2. The different modes for testing Python programs - interactive mode and script mode. Interactive mode allows writing code directly in the Python prompt while script mode uses separate .py files. 3. Input and output functions like print() and input() with examples of displaying output and accepting user input. 4. Tokens as the basic building blocks of Python programs including identifiers, keywords, operators, delimiters and literals. Identifiers must start with a letter or underscore.

Uploaded by

Aakaash C.K.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

12 CS Guide R.M.K.MATRIC.HR.SEC.

SCHOOL - 601206

Namma Kalvi
UNIT CHAPTER 5 PHYTHON-VARIABLES AND OPERATORS
II
www.nammakalvi.in
Python is a general purpose programming language Now Python IDLE window appears
created by Guido Van Rossum  The prompt (>>>) indicates that Interpreter is ready
1.What are the Key features of Python to accept instructions.
 It is a general purpose programming language.  Therefore, the prompt on screen means IDLE is
 It can be used for both scientific and non-scientific working in interactive mode.
programming. 5.Describe in detail the procedure Script mode
 It is a platform(OS) independent programming programming.
language.  A script is a text file containing the Python
 The programs written in Python are easily readable statements.
and understandable.  Python Scripts are reusable and editable code.
2.Use of Python IDLE  Once the script is created, it can be executed again
 The version 3.x of Python IDLE (Integrated and again without retyping.
Development Learning Environment) is used to (i) Creating Scripts in Python
develop and run Python code.  Choose File → New File or press Ctrl + N in Python
 It can be downloaded from the web resource shell window.
www.python.org.  An untitled blank script text editor will be displayed
3.What are the different modes that can be used to on screen
test Python Program ?  Type the code in Script editor
 In Python, programs can be written in two ways For example
namely Interactive mode and Script mode. a =100
 The Interactive mode allows us to write codes in b = 350
Python command prompt (>>>) directly and the c = a+b
interpreter displays the result(s) immediately. print ("The Sum=", c)
 The interactive mode can also be used as a simple (ii) Saving Python Script
calculator.  Choose File → Save or Press Ctrl + S
 In script mode programs can be written and stored  Now, Save As dialog box appears on the screen
as separate file with the extension .py and  In the Save As dialog box, select the location and
executed. Script mode is used to create and edit type the file name in File Name box.
python source file.  Python files are saved with extension .py.
4.Describe in detail the procedure Interactive mode Finally, click Save button to save your Python script.
programming. (iii) Executing Python Script
 The Interactive mode allows us to write codes in Choose Run → Run Module or Press F5
Python command prompt (>>>) directly and the  If your code has any error,
interpreter displays the result(s) immediately.  it will be shown in red color in the IDLE window,
 The interactive mode can also be used as a simple and describes the type of error occurred.
calculator.  To correct the errors, go back to Script editor, make
Invoking Python IDLE corrections, save the file using Ctrl + S or File →
Start -> All Programs ->Python 3.x->IDLE(Python 3.x) Save and execute it again.
 The output will appear in the IDLE window of
Click python icon on the desktop Python from error free code

Elangovan 9677515019 Page 1


12 CS Guide R.M.K.MATRIC.HR.SEC.SCHOOL - 601206

6.Input and Output Functions x = int (input(“Enter Number : ”))


 The input() function helps to enter data at run time print (“The Number = ”, x)
by the user . Output:
 In Python, the print() function is used to display Enter Number : 34
result on the screen. The number = 34
7.Explain print() functions with examples x,y=int (input("no 1 :")),int(input("no 2:"))
 In Python, the print() function is used to display print ("X = ",x," Y = ",y)
result on the screen. Output:
 The syntax for print() is as follows: No1:56
Example No 2: 65
print (“string to be displayed as output ” ) X=56 Y=65
print (variable ) How input values explicitly converted into numeric data
print (“String to be displayed as output ”, variable) type? Example.
print (“Str1 ”, var1, “Str 2”, var2, “Str 3” ……)  The input values should be explicitly converted into
Example numeric data type.
>>>print (“Welcome to Python”)  The int( ) function is used to convert string data as
Welcome to Python integer data explicitly.
>>> x = 5  float() is used for float data
>>>y = 10 x = int (input(“Enter Number : ”))
>>> print (x) print (“The Number = ”, x)
5 Output:
>>> print (“The No is = ”, x) Enter Number : 34
The No is =5 The number = 34
>>> print (“The sum of ”, x, “ and ”, y, “ is ”, z) x,y=int (input("no 1 :")),int(input("no 2:"))
The sum of 5 and10 is 15 print ("X = ",x," Y = ",y)
 Comma ( , ) is used as a separator in print ( ) to Output:
print more than one item. No1:56
8.Explain input() functions with examples No 2: 65
 In Python, input( ) function is used to accept data as X=56 Y=65
input at run time. 9.Define Comments in Python
Syntax is  In Python, comments begin with hash symbol (#).
Variable =input (“prompt string”)  The lines that begins with # are considered as
 prompt string is used, to display statement or comments and ignored by the Python interpreter.
message on the monitor. Two types of Comments, 1.single line 2.multi-lines.
Example  The multiline comments should be enclosed within
input( ) with prompt string a set of #
>>> city=input (“Enter Your City: ”) Ex..
Enter Your City: chennai # It is Single line Comment
input( ) without prompt string # It is multiline comment
>>> city=input() which contains more than one line#
elango
 The input ( ) accepts all data as string or characters
but not as numbers. 10.What is the use of whitespace in python ?
 If a numerical value is entered,  Python uses whitespace such as spaces and tabs to
 The input values should be explicitly converted into define program blocks
numeric data type.
 The int( ) function is used to convert string data as
integer data explicitly.
 float() is used for float data

Elangovan 9677515019 Page 2


12 CS Guide www.nammakalvi.in R.M.K.MATRIC.HR.SEC.SCHOOL - 601206

11.What are Tokens?  % (Modulus) >>> a % 30


 Python breaks each logical line into a sequence  ** (Exponent) >>> a ** 2
of elementary lexical components known as  // (Floor Division) >>> a//30(integer
Tokens. division)
The normal token types are Relational or Comparative operators
1) Identifiers,  A Relational operator is also called as
2) Keywords, Comparative operator.
3) Operators,  It is used to check the relationship between two
4) Delimiters and operands.
5) Literals.  If the relation is true, it returns True; otherwise it
12.Define Identifier? returns False.
 An Identifier is a name used to identify a variable, == (is Equal)
function, class, module or object. > (Greater than)
Rules < (Less than)
 An identifier must start with an alphabet (A..Z or >= (Greater than or Equal to)
a..z) or underscore ( _ ). <= (Less than or Equal to
 Identifiers may contain digits (0 .. 9) != (Not equal to)
 Python identifiers are case sensitive i.e. uppercase Logical operators
and lowercase letters are distinct.  Logical operators are used to check relational
 Identifiers must not be a python keyword. between two relational expressions(condition)
 Python does not allow punctuation character such  There are three logical operators they are and, or
as %,$, @ etc., within identifiers. and not.
13.What are Keywords? a,b=10,5
 Keywords are special words used by Python print(a>b)
interpreter to recognize the structure of program.
 As these words have specific meaning for output: TRUE
interpreter, they cannot be used for any other Assignment operators
purpose.  In Python, = is a simple assignment operator to
assign values to variable
a=10
print(a)

output: 10
Conditional operator
14.What is operands?  Ternary operator is also known as conditional
The value of an operator used is called operands. operator
15.Explain the types of operators used in python?  It is used to evaluate something based on a
condition being true or false.
Write short notes on Arithmetic operator with Syntax:
examples.  Variable Name = [on_true] if [Test expression] else
Arithmetic operators [on_false]
Ex. min= 50 if 49<50 else 70
 An arithmetic operator is a mathematical operator
that takes two operands and performs a calculation
# min = 50
on them.
 They are used for simple arithmetic.
Delimiters
 + (Addition) >>> a + b
Python uses the symbols and symbol combinations as
 (Subtraction) >>>a – b
delimiters in expressions, lists, dictionaries and strings.
 (Multiplication) >>> a*b
 / (Division) >>> a / b

Elangovan 9677515019 Page 3


12 CS Guide R.M.K.MATRIC.HR.SEC.SCHOOL - 601206

Python Data types


Following are the delimiters.  All data values in Python are objects
 Each object or value has type.
 Python has Built-in or Fundamental data types such
as Number, String, Boolean, tuples, lists and
dictionaries.
Number Data type
 integers, floating point numbers and complex
numbers are Number data type.
16.What is a literal? Explain the types of literals ? Integer Data
Literal  Integer Data can be decimal, octal or hexadecimal.
 Literal is a raw data given in a variable or constant. Ex.
 In Python, they are 1.Numeric 2. String 3.Boolean. 102, 4567, 567 # Decimal integers
Numeric Literals O102, o876, O432 # Octal integers
 Numeric Literals consists of digits and are OX102, oX876, OX432 # Hexadecimal integers
immutable (unchangeable). 34L, 523L # Long decimal integers
 3 different numerical types Integer, Float and (L Only Upper case)
Complex.
String Literals 18.Write short notes on Exponent data?
 In Python a string literal is a sequence of characters Floating point data
surrounded by quotes.  A floating point data is represented by a sequence
 Python supports single, double and triple quotes of decimal digits that includes a decimal point.
for a string.  An Exponent data contains decimal digit part,
 A character literal is a single character surrounded decimal point, exponent part followed by one or
by single or double quotes. T more digits.
 he value with triple-quote "' '" is used to give 123.34, 456.23, 156.23 # Floating point data
multi-line string literal. 12.E04, 24.e04 # Exponent data
Ex. Complex number
strings = "This is Python" Complex number is made up of two floating point
char = "C" values, one each for the real and imaginary parts.
Boolean Literals Ex .2+3j
real 2.0 imag 3.0
 A Boolean literal can have any of the two values:
Boolean Data type
True or False.
A Boolean data can have any of the two values:
 Ex. boolean_1 = True True or False.
17.Write short note on Escape Sequences.
Escape Sequences
 In Python strings, the backslash "\" is a special
character, also called the "escape" character.
 It is used in representing certain whitespace
characters:
 "\t" is a tab, "\n" is a newline, and "\r" is a
carriage return.
For example to print the message "It's raining", the
Python command is
>>> print ("It\'s rainning")
It's raining

Elangovan 9677515019 Page 4


12 CS Guide www.nammakalvi.in R.M.K.MATRIC.HR.SEC.SCHOOL - 601206

UNIT CHAPTER 6 CONTROL STRUCTURES


II ALGORITHMIC STRATEGIES
Example:
1.What is Control statements? List the control x=int (input())
structures in Python if x>=18:
print ("You are eligible for voting")
 A program statement that causes a jump of control >>> 20
from one part of the program to another is called You are eligible for voting
control structure or control statement. >>>13
There are three important control structures >>>
 Sequential  In the second execution no output will be printed
 Alternative or Branching Because simple if execute only true statement.
 Iterative or Looping (ii) if..else statement
2.Define sequential statement  The if .. else statement provides control to check
 A sequential statement is composed of a sequence the true block as well as the false block.
of statements which are executed one after
another if condition :
Ex. True block statements
print(“Elango”) else :
Print(“Chennai”) False block statements
3.What is alternative or branching statements?
Explain simple if statement with an example.  if the condition is true , True block statements will
Explain if …else.statement with an example. be executed
Explain if …elif…else statement with an example.  if the condition is False , False block statements will
be executed
Example:
 In a program, to skip a segment or set of statements n=int(input("Enter a Number : "))
and execute another segment based on the test if n<0:
condition. print("Enter only Positive Number")
else:
 Condition should be in the form of relational or
f=1
logical expression
for i in range(1,n+1):
They are f=f*i
1.Simple if statement print("The Factorial is:",f)
2. if..else statement Output
3. if..elif statement Enter a Number : 5
(i) Simple if statement The Factorial is: 120
 Simple if is the simplest of all decision making
statements. Enter a Number : -5
Syntax: Enter only Positive Number
An alternate method to write complete if .. else
if condition :
True block statements Var = var 1 if condition else var 2

 if the condition is true , True block statements will  The condition is checked,
be executed  if it is true, the value of var1 is stored in var
 otherwise ,the value of var2 is stored in var.
Elangovan 9677515019 Page 1
12 CS Guide R.M.K.MATRIC.HR.SEC.SCHOOL - 601206

x=int (input())
v= print ("You are eligible for voting") if x>=18
else print ("You are not eligible for voting") not in statement
>>> 20
You are eligible for voting
if ch in (‘a’, ‘A’, ‘e’, ‘E’, ‘i’, ‘I’, ‘o’, ‘O’, ‘u’, ‘U’):
>>>13 print (ch,’ is not a vowel’)
You are not eligible for voting
>>>Enter a character : p
iii) Nested if..elif...else statement:
 When we need to construct a nested if statement(s) p is not a vowel
then ‘elif’ clause can be used instead of ‘else’. 5.Define Iteration or Looping constructs
syntax:  Iteration or loop are used to execute a block of code
several of times or till the condition is satisfied.
if condition 1: Python provides two types of looping constructs:
block statements 1  while loop
elif condition 2:  for loop
block statements 2 6.Explain while loop with an example
else :
block statements 3 In the while loop,
 The condition is in valid Boolean expression ,
 Returning True or False
 ‘elif’ can be considered to be abbreviation of  while loop is entry check loop type,
‘else if’.  It is not executed even once if the condition is
 In an ‘if’ statement there is no limit of ‘elif’ clause, tested False in the beginning.
but an ‘else’ clause should be placed at the end. Syntax:
In the syntax of if..elif..else,
 condition-1 is tested intialization
if it is true then block statements 1 is executed, while condition :
 otherwise the control checks condition-2, statements block 1
if it is true block statements 2 is executed updation
 otherwise else part is executed else: (optional)
Example: statements
m=int (input(“Enter mark: ”))
if m>=80: In the while loop,
print (“Grade : A”)  The condition is in valid Boolean expression ,
elif m>=70 and m<80:  Returning True or False
print (“Grade : B”)  The condition is checked in the beginning.
elif m>=60 and avg<50:  If it is TRUE the body of the loop(statements-block
print (“Grade : C”) 1) is executed otherwise else is execute.
else:  The else is optional part of while.
print (“Grade : E”) Example:
output n=int(input("Enter a value of n : "))
>>> Enter mark : 75 s=0
Grade :B i=1
while(i<=n):
4.Give an example of in and not in statements. a=(i**i)/i
In statement i=i+1
ch=input (“Enter a character :”) s=s+a
if ch in (‘a’, ‘A’, ‘e’, ‘E’, ‘i’, ‘I’, ‘o’, ‘O’, ‘u’, ‘U’): print("The sum of the series is..",s)
print (ch,’ is a vowel’)
>>>Enter a character : a
a is a vowel

Elangovan 9677515019 Page 2


12 CS Guide www.nammakalvi.in R.M.K.MATRIC.HR.SEC.SCHOOL - 601206

 Indentation is used to creates blocks and sub-


7.Explain for loop with an example blocks
Write note on range () in loop 9.Explain Nested loop with an example.

 for loop is the most comfortable loop.  A loop placed within another loop is called as
 It is also an entry check loop. nested loop structure.
 The condition is checked in the beginning.  One can place a while within another while;
 If it is TRUE, the body of the loop(statements-block  for within another for;
1) is executed  for within while
 otherwise the loop is not executed  while within for to construct such nested loops.

Syntax: Syntax:

for counter_variable in sequence: Initial


statements-block 1 While condition :
else: – (optional) for counter_variable in sequence:
statements statements-block 1
updating
counter variable
 A variable used in a loop to count the number of Example:
times something happened. i=1
Sequence while (i<=6):
 The sequence refers to the initial, final and for j in range (1,i):
increment value. print (j,end='\t')
 In Python, for loop uses the range() function to the print (end='\n')
sequence. i +=1
 range() generates a list of values starting from start Output:
till stop-1. 1
Syntax of range() 12
range (start,stop,step) 123
Where, 1234
 start – refers to the initial value (0 is default) 12345
 stop – refers to the final value (stop-1) 10.Explain Jump statements in python.
 step – refers to increment value (1 is default) List the differences between break and continue
start and step are optional part. statements
Example : Define break, statement,pass statements
n = int(input("Enter a value of n: "))
s=0 The jump statement in Python, is used to transfer the
for i in range(1,n+1): control from one part of the program to another with
a=(i**i)/i out condition.
s=s+a There are three keywords: break, continue, pass.
print("The sum of the series is ", s) i) break statement
Output:  The break statement terminates the current loop .
Enter a value of n: 5  When the break statement is executed,
The sum of the series is 701.0 The control comes out of the loop
8.What is the importance of indentation in python? and starts executing after the loop structure.
 In Python, indentation is important in control  In nested loop, break will terminate the innermost
statements. loop.
 if a loop is left by break, the else part is not
executed’.
Elangovan 9677515019 Page 3
12 CS Guide R.M.K.MATRIC.HR.SEC.SCHOOL - 601206

Ex.
for word in “Jump Statement”: 1. Write a program to display
A
if word = = “e”: AB
break ABC
print (word, end= ' ') ABCD
ABCDE
output : Jump Stat for c in range(65,70):
(ii) continue statement for a in range (65,c+1):
 Continue statement is used to skip the remaining print (chr(a),end=’ ‘)
part of a loop and start with next iteration. print(end=’\n)
for word in “Jump Statement”: c+=1
if word = = “e”:
continue 2. Using if..else..elif statement write a suitable program
print (word, end= ' ') to display largest of 3 numbers.
a,b,c=int(input()),int(input()),int(input())
Output : Jump Statmnt if a>b and a>c:
print("A is greater")
Note: except the letter ‘e’ all the other letters get
elif b>a and b>c:
printed.
print("B is greater")
else:
(iii) pass statement
print("C is greater")
 pass statement in Python is a null statement.
 Completely ignored by interpreter. 3. Write a program to display all 3 digit odd numbers.
 pass statement is generally used as a placeholder for i in range(101,1000,2):
print(i,end=' ')
 pass statement used to construct a body that does
4. Write a program to display multiplication table for a
nothing
given number.
 Nothing happens when pass is executed, it results in
n=int(input())
no operation.
for i in range(1,11):
 pass statement can be used in ‘if’ or loop construct,
print(n*i)
Ex,
for val in “Computer”:
pass
print (“loop structure will be built in future”)
Write a note on the parameters used in print ()
statements.
Difference between end and sep
print can have end, sep as parameters.
 end parameter can be used to give any escape
sequences like ‘\t’ for tab, ‘\n’ for new line and so
on.
 sep as parameter can be used to specify any special www.nammakalvi.in
characters like, (comma) ; (semicolon) as separator
between values .
What are the values taken by range()?
 It take values from string,list,dictionary.

Elangovan 9677515019 Page 4

You might also like