Chapter -3Brief Overview of Python

1. Who developed Python Programming Language ?
SOLUTION:-
Guido Van Rossum in 1990s developed Python programming language.

2. Is Python an Object Oriented language?
SOLUTION:-
Yes, Python is an Object Oriented language.

3. Python is an interpreted high level language”. What does it mean to you?
SOLUTION:-
Python is a high level language’ means it is programmer-friendly fr., easy to program and comprehend. ‘Python is an interpreted language’ means it requires an interpreter (not compiler) to execute its code line by line-one statement at a time.

4. What does a cross platform language mean?
SOLUTION:-
A cross platform language means it can run well on variety of platforms like Windows, Linux/Unix, Macintosh etc. etc.
5. Python is a Free and Open Source language. What do you understand by this feature?
SOLUTION:-
It means to download Python, one needs not pay anything, because it is Free. And its source-code is also available, which can be modified/improved etc., because it is open-source.

6. What is the difference between interactive mode and script made in Python?
SOLUTION:-
In interactive mode, instructions are given in front of Python prompt (eg. >>> or In[]prompts) in Python Shell. Python carries out the given instruction and shows the result there itself. In script mode, Python instructions are stored in a file generally with .py extension and are executed together in one go as a unit. The saved instructions are known as Python script or Python program.
7. What is the difference between a keyword and an identifier ?
SOLUTION :-
Keyword is a special word that has a special meaning and purpose. Keywords are reserved and are a few. For example, if, elif, else etc. are keywords.
Identifier is a user-defined name given to a part of a program viz. variable, object, function etc. Identifiers are not reserved. These are defined by the user but they can have letters, digits and a symbol underscore. They must begin with either a letter or underscore. For instance, _chk, chess, trial etc. are identifiers in Python.

8.What are literals in Python? How many types of literals are allowed in Python?
SOLUTION :-
Literals mean constants i.e., the data items that never change their value during a program run. Python allows five types of literals:
(i) String literals
(ii) Numeric literals
(iii) Boolean literals
(iv) Special Literal None
(v) Literal Collections like tuples, lists etc.

9.How many types of strings are supported in Python?SOLUTION :-
Python allows two string types:
(i) Single-line Strings :- Strings that are terminated in single line
(ii) Multi-line Strings :- Strings storing multiple lines text.

10. What factors guide the choice of identifiers in programs?
SOLUTION :-
(i) An identifier must start with a letter or underscore followed by any number of digits and/or letters.
(ii) No reserved word or standard identifier should be used.
(iii) No special character (other than under-score) should be included in the identifier.

11. How can you create multi-line strings in Python?
SOLUTION :-
Multi-line strings can be created in two ways:
(a) By adding a backslash at the end of normalsingle-quote or double-quote strings e.g.,
Text =”Welcome \Το\Python”
(b) By typing the text in triple quotation marks. (No backslash needed at the end of line) e.g.,
Str1=”””WelcomeΤοPython”””
12.What is None literal in Python ?
SOLUTION :-
Python has one special literal called None.
The None literal is used to indicate something that has not yet been created in simple words, or absence of value. It is also used to indicate the end of lists in Python.

13. What are data types ? What are Python’s built-in core data types ?
SOLUTION:-

The real life data is of many types. So to represent various types of real-life data, programming languages provide ways and facilities to handle these, which are known as data types, Python’s built-in core data types belong to

Numbers (integer, floating-point, complex numbers, Booleans)
String
Tuple
List
Dictionary

14.Which data types of Python handle Numbers?
SOLUTION :-

Python provides following data types to handle numbers
(i) Integers
(ii) Boolean
(iii) Floating-point numbers
(iv) complex numbers

15. Why is Boolean considered a subtype of integers?
SOLUTION :- 

Boolean values True and False internally map to integers 1 and 0. That is, internally True is considered equal to 1 and False equal to 0 (zero). When I and 0 are converted to Boolean through bool() function, they return True and False. That is why Booleans are treated as a subtype of integers.

16.What do you understand by term ‘immutable?
SOLUTION:-
Immutable means unchangeable. In Python, immutable types are those whose values cannot be changed in place. Whenever one assigns a new value to a variable referring to immutable type, variable’s reference is changed and the previous value is left unchanged. e.g.
x=3
x=5

17. What do you mean by Syntax errors and Semantics errors?
SOLUTION :-
Syntax errors are the errors that occur when rules of a programming language violated.
Semantics errors occur when statements are not meaningful.

18.Why are logical errors harder to locate?
SOLUTION :-
In spite of logical errors presence, the program executes without any problems but the output produced is not correct. Therefore, each and every statement of the program needs to b scanned and interpreted. Thus the logical errors are harder to locate.

19. What is an Exception?
SOLUTION :-
Exception in general refers to some contradictory or unusual situation which can be encountered unexpectedly while executing a program.

20. What is a statement ? What is the significance of an empty statement ?
SOLUTION:-
A statement is an instruction given to the computer to perform any kind of action. An empty statement is useful in situations where the code requires a statement but logic does not. To fill these two requirements simultaneously, empty statement is used.Python offers pass statement as an empty statement.

21. Use the Python rangel) function to create the following list: [17, 3,-1, -5].
SOLUTION:-
range (7, -6, -4)

22.What is the purpose of range() function ? Given one example.
SOLUTION:-
The range() function generates an iterable sequence using the arguments start, stop and stop of range() function. It is very handy and useful for the “for loops, which require an iterable sequence for looping.

23. Write a note on Decision making in Python?
Solution :-
Python If-else statements
Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making.
Indentation in Python
For the ease of programming and to achieve simplicity, python doesn’t allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block.
Indentation is the most used part of the python language since it declares the block of code. All the statements of one block are intended at the same level indentation. We will see how the actual indentation takes place in decision making and other stuff in python.
The if statement :-
The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as if-block. The condition of if statement can be any valid logical expression which can be either evaluated to true or false.
The syntax of the if-statement is given below.
if expression:
statement
Example 1
num = int(input(“enter the number?”))
if num%2 == 0:
print(“Number is even”)
The if-else statement :-
The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition.
If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.
The syntax of the if-else statement is given below.
if condition:
#block of statements
else:
#another block of statements (else-block)
Example 1 : Program to check whether a person is eligible to vote or not.
age = int (input(“Enter your age? “))
if age>=18:
print(“You are eligible to vote !!”)
else:
print(“Sorry! you have to wait !!”)
The elif statement :-
The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. However, using elif is optional.
The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.
The syntax of the elif statement is given below.
if expression 1:
# block of statements

elif expression 2:
# block of statements

elif expression 3:
# block of statements

else:
# block of statements
Example 1
number = int(input(“Enter the number?”))
if number==10:
print(“number is equals to 10”)
elif number==50:
print(“number is equal to 50”);
elif number==100:
print(“number is equal to 100”);
else:
print(“number is not equal to 10, 50 or 100”);

24. What is range()funtion in Python?

Solution :-
The range() function
The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the range() function is given below.
Syntax:
range(start,stop,step size)
The start represents the beginning of the iteration.
The stop represents that the loop will iterate till stop-1. The range(1,5) will generate numbers 1 to 4 iterations. It is optional.
The step size is used to skip the specific numbers from the iteration. It is optional to use. By default, the step size is 1. It is optional.
Example-1: Program to print numbers in sequence.
for i in range(10):
print(i,end = ‘ ‘)

25.Write a note on Python Loops?
Solution :-
The flow of the programs written in any programming language is sequential by default. Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times.
For this purpose, The programming languages provide various types of loops which are capable of repeating some specific code several numbers of times. Consider the following diagram to understand the working of a loop statement.

Python for loop
The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary.
The syntax of for loop in python is given below.
for iterating_var in sequence:
statement(s)
The for loop flowchart
For loop Using Sequence
Example-1: Iterating string using for loop
str = “Python”
for i in str:
print(i)
Python While loop
The Python while loop allows a part of the code to be executed until the given condition returns false. It is also known as a pre-tested loop.
It can be viewed as a repeating if statement. When we don’t know the number of iterations then the while loop is most effective to use.
The syntax is given below.
while expression:
statements
Here, the statements can be a single statement or a group of statements. The expression should be any valid Python expression resulting in true or false. The true is any non-zero value and false is 0.
Loop Control Statements
We can change the normal sequence of while loop’s execution using the loop control statement. When the while loop’s execution is completed, all automatic objects defined in that scope are demolished. Python offers the following control statement to use within the while loop.
1. Continue Statement – When the continue statement is encountered, the control transfer to the beginning of the loop. Let’s understand the following example.
Example:
# prints all letters except ‘a’ and ‘t’
i = 0
str1 = ‘javatpoint’
while i < len(str1):
if str1[i] == ‘a’ or str1[i] == ‘t’:
i += 1
continue
print(‘Current Letter :’, a[i])
i += 1