Python Unit 2
Python Unit 2
It‟s become one of the most popular programming languages in recent history. The main
benefits of using Python instead of other languages are that it is very easy to start programming
with and because of its flexible syntax.
Python is an open source language that‟s free to use and has a wide range of features that
make it easy to customize. Some of the major features of Python are:
2. Interpreted Language
Python is an interpreted language and an IDLE is packaged along with Python. So when
running a python program, the code executes line after line dynamically.
6. High-Level Language
A high-level language (HLL) is a programming language that enables a programmer to
write programs that are more or less independent of a particular type of computer.
9. Platform Independent
Platform independence is yet another amazing feature of Python. In other words, it means
that if we write a program in Python, it can run on a variety of platforms, for instance, Windows,
Mac, Linux, etc. We do not have to write separate Python code for different platforms.
---
Q. what is python and its features
Q. Explain the features of python
---
History of python:
Python was created by Guido van Rossum, and first released on February 20, 1991.
While you may know the python as a large snake, the name of the Python programming
language comes from an old BBC television comedy sketch series called Monty Python’s Flying
Circus.
One of the amazing features of Python is the fact that it is actually one person‟s work.
Usually, new programming languages are developed and published by large companies
employing lots of professionals, and due to copyright rules, it is very hard to name any of the
people involved in the project. Python is an exception.
Of course, Guido van Rossum did not develop and evolve all the Python components himself.
The speed with which Python has spread around the world is a result of the continuous work of
thousands (very often anonymous) programmers, testers, users (many of them aren‟t IT
specialists) and enthusiasts, but it must be said that the very first idea (the seed from which
Python sprouted) came to one head – Guido‟s.
Python is omnipresent, and people use numerous Python-powered devices on a daily basis,
whether they realize it or not. There are billions of lines of code written in Python, which means
almost unlimited opportunities for code reuse and learning from well-crafted examples. What‟s
more, there is a large and very active Python community, always happy to help.
There are also a couple of factors that make Python great for learning:
It is easy to learn – the time needed to learn Python is shorter than for many other
languages; this means that it‟s possible to start the actual programming faster;
It is easy to use for writing new software – it‟s often possible to write code faster when
using Python;
It is easy to obtain, install and deploy – Python is free, open and multiplatform; not all
languages can boast that.
Programming skills prepare you for careers in almost any industry, and are required if you want
to continue to more advanced and higher-paying software development and engineering roles.
Python is the programming language that opens more doors than any other. With a solid
knowledge of Python, you can work in a multitude of jobs and a multitude of industries. And the
more you understand Python, the more you can do in the 21st Century. Even if you don‟t need it
for work, you will find it useful to know.
Python is the programming language that opens more doors than any other. With a solid
knowledge of Python, you can work in a multitude of jobs and a multitude of industries. And
even if you don‟t need it for work, you will still find it useful to know to speed certain things up
or develop a deeper understanding of other concepts.
Python is a great choice for career paths related to software development, engineering, DevOps,
machine learning, data analytics, web development, and testing.
---
Q. Write brief history of Python
---
Variables : for developing little complex programs, we need to take input from user , store them
in memory and do process on that data, for which we need to reserve some memory. This
reserved memory is called a variable and it is identified with an identifier (name). The python
variables are dynamic. The type of variable need not be mentioned as it is done in C, C++, java
etc languages. The type of variable is dynamically decided by the python interpreter during
running of program based on type of value assigned to it.
Unlike in other languages, in python we can assign multiple values to multiple variables at a
time.
We can write
a,b,c=5,6,7
instead of writing
a=5
b=6
c=7
---
Q. Explain how to write and execute a python program with basic syntaxes.
Q. Explain Building blocks / tokens of python
---
operators :
arithmetic operators :
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Exponentiation
// Floor division
comparision operators :
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Logical Operators:
and Returns True if both statements are true
or Returns True if one of the statements is true
not Reverse the result, returns False if the result is true
assignment Operators :
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
identity Operator:
membership operator :
in Returns True if a sequence with the specified value is present in the object x in y
not in Returns True if a sequence with the specified value is not present in the object x not in y
---
Q. Write about python operators.
---
Indentation :
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.
Other programming languages often use curly-brackets for this purpose.
Conditional statements:
Conditional Statement in Python performs different computations or actions depending on
whether a specific Boolean constraint evaluates to true or false. Conditional statements are
handled by IF statements in Python.
if :
Python if Statement is used for decision-making operations. It contains a body of code which
runs only when the condition given in the if statement is true.
if-else: when we need to divide program into 2 parts then we use if-else.
If the condition is true, then the statement under if will be executed and If the condition is false,
then the optional else statement runs which contains some code.
Ex:
x,y =8,4
nested if-else: if we need to place an if condition with in another if or else part, then it is called
nested if.
If-elif-else: if we get a requirement of dividing program into more than 2 parts, then we can use
elif statement. (it is like if-else-if in C, C++, java etc)
Ex:
If a>b && a>c:
print(„a is big‟)
elif b>c:
print(„b is big‟)
else:
print(„c is big‟)
---
Q. write about different if controlling statements.
---
Python programming language provides the following types of loops to handle looping
requirements. Python provides three ways for executing the loops. While all the ways provide
similar basic functionality, they differ in their syntax and condition checking time.
In python, a while loop is used to execute a block of statements repeatedly until a given
condition is satisfied. And when the condition becomes false, the line immediately after the loop
in the program is executed.
Syntax :
while expression:
statement(s)
All the statements indented by the same number of character spaces after a programming
construct are considered to be part of a single block of code. Python uses indentation as its
method of grouping statements.
Example:
count = 0
while (count < 3):
print(„count :‟, count)
count = count + 1
For loops are used for sequential traversal. For example: traversing a list or string or array etc. In
Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is
similar to for each loop in other languages. Let us learn how to use for in loop for sequential
traversals.
Syntax:
for iterator_var in sequence:
statements(s)
It can be used to iterate over a range and iterators.
n=4
for i in range(0, n):
print(i)
---
Q. write about different loops in python.
---
Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes,
there may arise a condition where you want to exit the loop completely, skip an iteration or
ignore that condition. These can be done by loop control statements. Loop control statements
change execution from its normal sequence. When execution leaves a scope, all automatic
objects that were created in that scope are destroyed. Python supports the following control
statements.
Break statement
Continue statement
Pass statement
Break statement:
The break statement is used to terminate the loop or statement in which it is present. After that,
the control will pass to the statements that are present after the break statement, if available. If
the break statement is present in the nested loop, then it terminates only those loops which
contains break statement.
Continue statement:
Continue is also a loop control statement just like the break statement. continue statement is
opposite to that of break statement, instead of terminating the loop, it forces to execute the next
iteration of the loop. As the name suggests the continue statement forces the loop to continue or
execute the next iteration. When the continue statement is executed in the loop, the code inside
the loop following the continue statement will be skipped and the next iteration of the loop will
begin.
Pass statement:
As the name suggests pass statement simply does nothing. The pass statement in Python is used
when a statement is required syntactically but you do not want any command or code to execute.
It is like null operation, as nothing will happen is it is executed. Pass statement can also be used
for writing empty loops. Pass is also used for empty control statement, function and classes.
---
Q. write about break, continue and pass keywords in python.
---