IX Ch-2 Introduction To Python (Python Manual)
IX Ch-2 Introduction To Python (Python Manual)
Q6) What are the different modes for coding in python? (page 29, 30)
Python shell can be used in two ways, interactive mode and script mode.
1. Interactive Mode, as the name suggests, allows us to interact with OS. Working in interactive mode is
convenient for beginners and for testing small pieces of code, as we can test them immediately.
2. Script mode lets us create and edit Python source files. For coding more than few lines, we should
always save our code so that we may modify and reuse the code.
Note: Result produced by Interpreter in both the modes, viz., Interactive and script mode is exactly the same.
Q7) What are comments in python? List down the various types of comments. (32, 33)
A comment is text that doesn't affect the output of a code, and ignored by the python interpreter.
It is just a piece of text to let someone know what you have done in a program or what is being done in a
block of code.
It is basically the internal documentation of the source code.
We can write comments in two ways:
1. Single Line comments, we use the hash (#) symbol to start writing a comment.
2. Multi-line comments, we start and end the multiline comments with triple (“““) quotes.
Q8) What is an identifier? What are the different properties of an identifier? (page 35)
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity
from another.
Properties or naming rules of an identifier:
Q10) Write short note on, Keywords and Statements with example of each. (page 32, 33)
Statements are the instructions written in the source code for execution. There are different types of
statements in the Python programming language like Assignment statement, Conditional statement, Looping
statements etc. These help the user to get the required output. For example, n = 50 is an assignment
statement.
Keywords are the reserved words in Python used by Python interpreter to recognize the structure of the
program. For example, if, elif and else are the keywords to implement the conditional structure in a program.
Q11) What is a constant? Give example. (page 37)
A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as containers
that hold information which cannot be changed.
It is achieved by creating a module with constants and later the module can be imported in other programs.
Constants are written in all capital letters and underscores separating the words.
• Create a main.py
import info
print(info.NAME) # accessing the constant variable defined in info.py
print(info.AGE) # value of nfo.NAME and info.AGE cannot be changed
Q12) What are the rules and conventions for naming of variables. (page 38)
Rules for naming variables and constants are same as identifier naming rules given in answer 8.
Conventions are the guidelines for good programming:
Q13) Explain python output function with the help of an example. (page 41)
We use the print() function to output data to the standard output device (screen).
An example is given below.
a = "Hello World!"
print(a)
OUTPUT:
Hello World!
a = "tarun"
print("My name is :",a)
OUTPUT:
My name is : tarun
x = 1.3
print("x = \n", x) # \n is to add a new line
OUTPUT:
x=
1.3
m = 6
print(" I have %d apples", m) #%d is the format specifier for integer variables
OUTPUT:
I have 6 apples
Q14) What are Python operators? Explain the Arithmetic operators. (page 41)
Operators are special symbols which represent computation. They are applied on operand(s), which can be
values or variables. Same operators can behave differently on different data types. Operators when applied on
operands form an expression. Operators are categorized as Arithmetic, Relational, Logical and Assignment.
Value and variables when used with operator are known as operands.
Different Arithmetic operators and the operation they perform:
Operator Meaning Example Working Output
+ Addition 20+10 Sum of 20 and 10 30
- Subtraction 20-10 10 is subtracted from 20 10
* Multiplication 20*10 20 is multiplied by 10 200
/ Division 33/10 Returns quotient (fractional) 4.3
// Integer Division 43/10 Returns quotient (integer) 4
% Modulus 43/10 Returns the remainder after division 3
** Exponent 3**3 Returns 3X3X3 27
Python statement:
print( 12 + 5 * (8 – 2) / 10)
Expression: 12 + 5 * 8 – 2 / 10
Operators: +, *, -, /
Operands: 12, 5, 8, 2, 10
OUTPUT:
The principal amount is 100000
The time period is 2
The rate of interest is 2.5
The Simple Interest is 5000.0
OUTPUT:
Temperature in Celsius: 99
Temperature in Fahrenheit: 210.2