0% found this document useful (0 votes)
35 views9 pages

Python Unit 2

svu bcom python unit 2

Uploaded by

rajanikanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
35 views9 pages

Python Unit 2

svu bcom python unit 2

Uploaded by

rajanikanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Unit – 2: Introduction to python

Python and features of python:

Python is a high-level, general-purpose, self-contained programming language designed


to meet the needs of computer scientists, software developers, and college students interested in
coding. Python was created in the early 1980s by Guido van Rossum.

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:

1. Easy To Learn and Readable Language


Python is extremely easy to learn. Its syntax is super simple and the learning curve of
Python is very smooth. It is extremely easy to learn and code in Python and the indentation used
instead of curly braces in Python makes it very easy to read Python code. Perhaps, because of
this a lot of schools and universities, and colleges are teaching Python to their students who are
beginning their journey with coding.

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.

3. Dynamically Typed Language


Python is a dynamically typed language. In other words, in Python, we do not need to
declare the data types of the variables which we define. It is the job of the Python interpreter to
determine the data types of the variables at runtime based on the types of the parts of the
expression. Though it makes coding easier for programmers, this property might create runtime
errors. To be specific, Python follows duck typing. It means that “If it looks like a duck, swims
like a duck and quacks like a duck, it must be a duck.”

4. Open Source And Free


Python is an open-source programming language and one can download it for free from
Python‟s official website. The community of Python users is constantly contributing to the code
of Python in order to improve it.

5. Large Standard Library


One of the very important features because of which Python is so famous in today‟s times
is the huge standard library it offers to its users. The standard library of Python is extremely large
with a diverse set of packages and modules like itertools, functools, operator, and many more
with common and important functionalities in them. If the code of some functionality is already
present in these modules and packages, the developers do not need to rewrite them from scratch,
saving both time and effort on the developer‟s end. Moreover, the developers can now focus on
more important things concerning their projects.

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.

7. Object Oriented Programming Language


Python supports various programming paradigms like structured programming,
functional programming, and object-oriented programming. However, the most important fact is
that the Object-Oriented approach of Python allows its users to implement the concepts of
Encapsulation, Inheritance, Polymorphism, etc.

8. Large Community Support


With one of the biggest communities on StackOverflow and Meetup, Python has gained
its popularity over the years. If we need any kind of help related to Python, the huge community
is always there to answer our queries. A lot of questions about Python have already been
answered on these sites and Python users can reference them as per requirement.

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
---

Writing and executing the python program:


Once python is installed into system, the python system includes libraries, PVM (python
Virtual Machine) etc. An IDLE is also installed into system. The IDLE is Integrated
Development and Learning Environment.
We can run a python program from command prompt and also through the IDLE. The
execution of python program through IDLE is further of two types. Interactive interpreted mode
and interpreted mode.
In interactive interpreted mode, the code is written line after line and the each line
executed once programmer hits enter. The variables and values are carried forward till
programmer restarts the SHELL.
Otherwise, we create a file, save code with extension .py and execute entire code at once
in IDLE. (simply press F5 to run the python script).

Basic syntaxes in python:

print() : we use this function to produce output onto monitor.

input() : this function is used to read data from key board.


We can use these functions in 2 ways….
print(„enter name‟)
name=input()
Or
name= input(„enter name‟)

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

Data types : the different data types of python are :

Text Type: str

Numeric Types: int, float, complex

Collections list, tuple,set, dictionary

Boolean Type: bool


Keywords : keywords are important words of a language reserved for a specific purpose. There
are several keywords available in python as we have in other languages. All these must be typed
in lowercase letters. Ex: if, else, as, in etc

---
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:

is Returns True if both variables are the same object x is y


is not Returns True if both variables are not the same object x is not y

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

if(x < y):


st= "x is less than y"
else:
st= "x is greater than y"
print (st)

in python, it can also be written as :


x,y = 10,8
st = "x is less than y" if (x < y) else "x is greater than or equal to y"
print(st)

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.
---

looping statements-for, while,

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

we can also use an else block with while.


count = 0
while (count < 3):
print(„count :‟, count)
count = count + 1
else:
print("In Else Block")

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.
---

break, continue, pass :

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.
---

You might also like