Python Programming 1
Python Programming 1
Python Programming 1
Programming
Introduction to
Python
General Purpose
01 python language is designed for developing software that applies in a wide
range of application domains
Interpreted
02 python uses an interpreter to translate the instruction into machine
lan. guage line by line.
Object – Oriented
04 Python is a model of programming which aims to work with the real-world entities in
programming. Such as inheritance, data hiding, polymorphism etc. The main concept
of OOP (Object Oriented Programming) is to bind a specific portion of data and
functions together so that no other part, of rest of the code, can access them
History of Python
The implementation of Python
was started in December
Python 1.0 was
1989 by Guido Van released with
Python 3.8
Rossum at CWI in new features.
was released
Netherlandbeautifully
designed. 1994 2019
1989
1991 2008
1980
Guido Van Python 3.0 (also called
Python laid its
Rossum published the "Py3K") was released. It
foundation in the
code (labeled version was designed to rectify
late 1980
0.9.0) to alt.sources. the fundamental flaw of
the language.
Python programming language is being updated regularly with new features and supports. There are lots
of update in Python versions, started from 1994 to current release.
A list of Python versions with its released date is given below.
START
Software Engineering
Development Education
Application
FINISH
Business Enterprise
Applications.
Applications.
Key Features of Python
Easy to Learn - It is very easy to learn and understand. People of any age group can learn this language very easily. Its
language syntax is very simple to understand.
Free and Open Source - It is a free and open-source programming language and its source code is also available to the
public. You don't have to acquire any licence for its uses.
Interpreted Language - An interpreter is a translator that translates the code line by line into machine level language
and this makes it easier to debug the code. Python uses an interpreter to translate the program from a high-level language
to a machine level language so that the computer can understand our instruction.
Large Standard Library - A Large Standard Library of already written modules and functions of Python is available in
the market, making the coding easier and quicker in every aspect.
Object-Oriented Language - It supports object-oriented programming which includes concepts of classes, data
abstraction, polymorphism, encapsulation etc.
GUI Programming Support - Python supports programming in Graphical Users Interfaces using already existing
modules, such as PyQT5, wxPython, Tickler etc.
Portable Language - Python supports portability for example if we have a python code for windows and we want to
run this code on another platform such as, Unix, Linux or Mac then we do not need to change it, we can run this code on
any platform.
High-Level Language - Python is a high-level language. It means that it is easy to code, and once we write programs in
python, we do not need to worry about the system architecture, nor do we need to manage the memory in python manually.
Support Language Integration - Python supports language integration because we can easily integrated python with
other languages like C, C++ etc.
Installation of Python
Installation on Windows
Visit the link https://www.python.org/downloads/ to download the latest release of Python. In this
process, we will install Python 3.8.6 on our Windows operating system. When we click on the above
link, it will bring us the following page.
.
use free software for Python programming. This type of software is known as an IDE (Integrated Development
Environment).
Free IDE software available on the internet for Python programming. You can download any of the software
mentioned below for Python programming.
Spyder
PyDev
IDLE
(This is
the
default
print() Function and its Use
The print() function is used in the Python program to print text as well as the value of the
variables on the screen.
Example :
print('Hello World')
print("Hello World")
Output :
Hello World
Hello World
Example :
print('Sunday')
print('Monday')
OUTPUT :
Sunday
Monday
VARIABLES IN PYTHON
Whenever a variable is defined in Python, the interpreter allocates some memory according to the data
stored in the variable.
x=5
x=4.23
Explanation : we have declared a variable x and assigned the value 5 in it so, it becomes an integer variable. We can use the
same variable to store another type of value in it as you can see, on line number 2 we have used the same variable name x to
store a float value 4.23 in it so, now the variable x becomes a float variable
2. To declare a string variable in python.
y=“Python”
Explanation : we have declared a variable y and assigned a string value python in it so, it becomes a string
variable .
3. To declare a boolean variable in python.
z=True
Explanation : we have declared a variable z and assigned a boolean value True in it so, it becomes a boolean variable.
4. To declare multiple variables and assign multiple values in a single line.
a, b, c, d = 5, 4.26, "apple", True
a=5; b=4.26; c="apple"; d=True
Explanation :We have declared 4 variables a, b, c, d and assigned values 5, 4.26, "apple", True respectively in a single
line in two different ways, one using comma (,) and another using semicolon (;).
We Can use any one method that you like. So the value of a is 5, value of b is
4.23, value of c is apple and value of d is True which becomes a boolean variable.
5. To declare same value in multiple variables in a single
line. a=b=c=15
Explanation : We have declared 3 variables a, b, c and assigned the same value 15 in all the variables
respectively in a single line.
Text Editor - A text editor is a software used for writing code in Python. Example Windows Notepad,
Notepad++, vim for Linux.
Python Interpreter - A Python interpreter is a software required to interpret your source code into
machine-executable code.
PRINT() FUNCTION : The print() function is used in the Python program to print text as well as the value of the
var iables on the screen.
1. Program to print Sunday and Monday on separate lines using single print() function.
print('Sunday\nMonday') print('\
nSunday\n\n\nMonday')
OUTPUT :
Sunday
Monday
Sunday
Monday
2. (Use of sep
(separator)
parameter in
print()
function)
Python program to show the use of sep parameter in print() function.
print('Sunday','Monday',sep='-')
print('18','06','2020',sep='/')
print('is Thursday')
3. (Use of end parameter in print() function)
Python program to show the use of end parameter in print() function.
print('Sunday','Monday',end='-')
print('Tuesday')
print('Wednesday')
OUTPUT :
Sunday Monday-Tuesday
Wednesday
4. (Use of sep and end parameters together in print() function)
print(‘sudha',’nmkrv',sep='',end='@')
print(‘gmail.com')
OUTPUT :
[email protected]
5. Python program to print integer and float data type values on the screen. a=10
b=20.32
print('a=',a,' b=',b,sep='',end='\n\n')
print('a=',a,sep='')
print('b=',b,sep='')
OUTPUT :a=10 b=20
a=10
b=20.32
Print Python Variables and Literals
Program :
number = -10.6
name = "Programiz"
# print literals
print(5)
# print variables
print(number)
print(‘the value of the variable number is ‘ , number)
print(name)
OUTPUT :
5
10.6
Programiz
Print Concatenated Strings
We can also join two strings together inside the print() statement
print('Python is ' + 'awesome.')
OutPut :
Python is awesome
Output formatting
Sometimes we would like to format our output to make it look attractive.
This can be done by using the str.format() method.
x=5
y = 10
input ( prompt )
raw_input ( prompt )
input (): This function first takes the input from the user and converts it into a string. The
type of the returned object always will be <class ‘str’>. It does not evaluate the expression
it just returns the complete statement as String
Input(prompt)
prompt is the string we wish to display on the screen. It is optional
# using input() to take user input
num = input('Enter a number: ')
Enter a number: 10
You Entered: 10
Operators :
Operators are special symbols that perform operations on variables (name of the values ) and values
Example :
print(5 + 6)
>> 11
Here + is a operator and 5 , 6 are values
Example :2 :
x=10
y=12
print(x+y)
1. Arithmetic operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Special Operators
1. Python Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. For example,
Operator Description
+ (Addition) It is used to add two operands. For example, if a = 10, b = 10 => a+b = 20
- (Subtraction) It is used to subtract the second operand from the first operand. If the first
operand is less than the second operand, the value results negative. For
example, if a = 20, b = 5 => a - b = 15
/ (divide) It returns the quotient after dividing the first operand by the second operand.
For example, if a = 20, b = 10 => a/b = 2.0
* (Multiplication) It is used to multiply one operand with the other. For example, if a = 20, b = 4
=> a * b = 80
% (reminder) It returns the reminder after dividing the first operand by the second operand.
For example, if a = 20, b = 10 => a%b = 0
** (Exponent) As it calculates the first operand's power to the second operand, it is an
exponent operator.
// (Floor division) It provides the quotient's floor value, which is obtained by dividing the two
operands.
a=7
Program to perform Arithmetic b=2
operation: # addition (Comment Statement )
print ('Sum: ', a + b)
# subtraction
print ('Subtraction: ', a - b)
# multiplication
print ('Multiplication: ', a * b)
# division
print ('Division: ', a / b)
# floor division
print ('Floor Division: ', a // b)
# modulo
print ('Modulo: ', a % b)
# a to the power b
print ('Power: ', a ** b)
1. Write a python program to add three numbers .
2. Write a Python program to calculate the simple interest of an amount :
Formula : (pxnxr ) / 100.
3. Write a python program to calculate the area of a circle :
Formula : (3.14 x r2)
4. Write a python program to calculate area of a square:
Formula : a2
5. Write a python program to add 5 subjects marks and find the average.
6. Write a python program to find the percentage of the above total marks obtained by the student.
7. Write a python program to calculate the area of a triangle :
Formula : 1/2xbxh (b is breath and h is height )
2. Python Assignment Operators
Assignment operators are used to assign values to variables. For example,
Var x=5
list of different assignment operators
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= The condition is met if the first operand is smaller than or equal to the second
operand.
>= The condition is met if the first operand is greater than or equal to the second
operand.
> If the first operand is greater than the second operand, then the condition becomes
true.
< If the first operand is less than the second operand, then the condition becomes tru
a=5
b=2
# equal to operator
print('a == b =', a == b) OUTPUT :
a == b = False
# not equal to operator
a != b = True
print('a != b =', a != b) a > b = True
# greater than operator a < b = False
print('a > b =', a > b) a >= b = True
# less than operator a <= b = False
print('a < b =', a < b)
# greater than or equal to operator
print('a >= b =', a >= b)
# less than or equal to operator
print('a <= b =', a <= b)
List of Formatting Symbols
list of symbols used with % operator for formatting outputs on the screen.