Python Programming 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

PYTHON

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.

High level Language


03  python is very easy to learn and closer to human-readable
language, which is easy to understand

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.

Python Version Released Date Python Version Released Date


Python 1.0 January 1994 Python 2.7 July 3, 2010
Python 1.5 December 31, 1997 Python 3.0 December 3, 2008
Python 1.6 September 5, 2000 Python 3.1 June 27, 2009
Python 2.0 October 16, 2000 Python 3.2 February 20, 2011
Python 2.1 April 17, 2001 Python 3.3 September 29, 2012
Python 2.2 December 21, 2001 Python 3.4 March 16, 2014
Python 2.3 July 29, 2003 Python 3.5 September 13, 2015
Python 2.4 November 30, 2004 Python 3.6 December 23, 2016
Python 2.5 September 19, 2006 Python 3.7 June 27, 2018
Python 2.6 October 1, 2008 Python 3.8 October 14, 2019
Applications of Python

START

Web and Internet Scientific and


Development. Numeric Operating.

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

Step - 1: Select the Python's version


to download.
Click on the download button
Step - 2: Click on the Install Now
Double-click the executable file, which is
downloaded; the following window will
open.
Select Customize installation and
proceed.
Click on the Add Path check box, it will set
the Python path automatically.
Step - 3 Installation in Process
Now, try to run python on
the command prompt.
Type the command
python -version in case
of python3.
download
and
install
Python Download Pydroid 3 – IDE for Python
Latest 3 app from Play store
Version To install Pydroid app go to play store
on link here –
Android
After installation is
complete, Run the app
and it will show as
installing python.
Working with python
Input and output in
Python
Environment Setup for Coding in Python
Environment Setup is the process of installation and configuration of software required for coding programs in Python.
To start coding in Python we need two things:
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.

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

• A variable in Python is just a human-readable name used to store


data in computer memory.

• A variable is also used in a computer program to store, read and


change the data in computer memory.

• A variable is also known as Identifier because it is used for


identifying values in a computer program.

• When you declare a variable in Python programming language, it


means that you are asking the operating system to reserve a piece of
memory with that variable name for storing data.
What is meant by
Data is a collection ofData
facts or figure or combination of both (facts
and figure).
Facts - Any word that is consist of only alphabets is known as Facts.
Example: john, mango, book, etc these words are consist of only
alphabets. So each of them is a data but consist of Facts.
Figure - Any number whether it is whole number or real number is a
Figure.
Example: 15, 84.13, 5486, etc. So each of them is a data but consist of
Figure.
Combination of Both (Facts and Figure) - Anything which is consist of
Facts and Figure is known as Alphanumeric Text.
Example: 13street, alpha56, dx14c5, etc. So each of them is a data
but consist of Alphanumeric Text.
Note : Like other programming languages like C , C++,
Python do not have any commands to create
What is Data Type
Data Type in Python is used to classify the value stored in a variable during the program execution.

Whenever a variable is defined in Python, the interpreter allocates some memory according to the data
stored in the variable.

Python supports the following data types given below.


Data Types in Python
Integer - It is used to storewhole numbers such as 56, 8, 493 etc.
Complex Number - It is used to store complex number which is created from real numbers such as 9+21j,
-3-9j, -72+81j etc. A complex number has two parts a real part and an imaginary part
. Float - It is used to storenumbers with a decimal point such as 2.3, 18.12, 294.0 etc.
Boolean - It is used to store logical values such as True or False.String - It is used to store text such as
"hello", "Peter", "4586" etc.
Dictionary- It is used to store an unordered key and its value pairs such as {"name" : "frankie" , "age" :
"18"}.
Set - It is used to store an unordered and unindexed collection of unique objects such as {"a", "b“, "c"}.
List - It is used to storean ordered and changeable collection of objects such as ["apple", "banana", "cherry"].
Tuple - It is used to storean ordered and unchangeable collection of objects such as (15, "Albert", 18.20)
1. To declare an integer and float variable in python.

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.

INPUT AND OUTPUT IN PYTHON

Environment Setup for Python Programming


Environment Setup is the process of installation and configuration of softwarerequired for coding
programs in Python. To start coding in Python we need two things:

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

print('The value of x is {} and y is {}'.format(x,y))


Python Input :
Python provides us with two inbuilt functions to read the input from the keyboard.

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

print('You Entered:', num)

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)

Types of Python Operators


The different types of python operators are as follows :

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


+ Addition 5+2=7
- Subtraction 4-2=2
* Multiplication 2*3=6
/ Division 4/2=2
// Floor Division 10 // 3 = 3
% Modulo 5%2=1
** Power 4 ** 2 = 16
a. Arithmetic Operators
Arithmetic operations between two operands are carried out using arithmetic operators.

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


= Assignment Operator a=7
+= Addition Assignment a += 1 # a = a + 1
-= Subtraction Assignment a -= 3 # a = a - 3
*= Multiplication Assignment a *= 4 # a = a * 4
/= Division Assignment a /= 3 # a = a / 3
%= Remainder Assignment a %= 10 # a = a % 10
**= Exponent Assignment a **= 10 # a = a ** 10
Operator Description
= It assigns the value of the right expression to the left operand.
+= By multiplying the value of the right operand by the value of the left operand, the left
operand receives a changed value. For example, if a = 10, b = 20 => a+ = b will be equal
to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and assigns the
modified value back to left operand. For example, if a = 20, b = 10 => a- = b will be
equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and assigns the
modified value back to then the left operand. For example, if a = 10, b = 20 => a* = b
will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and assigns the
reminder back to the left operand. For example, if a = 20, b = 10 => a % = b will be equal
to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16
to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.
# assign 10 to a (Comment statement : this statement will not be printed on the screen )
a = 10
# assign 5 to b
b=5
# assign the sum of a and b to a
a += b # a = a + b
print(a)
a -= b #a=a–b
print(a)
a *= b # a = a * b
print(a)
a %= b # a= a% b
print(a)
a **=b # a= a ** b
print(a)
a//=b # a= a//b
print(a)
Comparison operator
Comparison operators compare the values of the two operands and return a true or false Boolean value in
accordance. The following table lists the comparison 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.

Formatting Symbols Description


%d Used for integer printing
%i Used for integer printing
%f Used for decimal number (float) printing
%c Used for character printing
%s Used for string (text) printing
%o Used for octal number printing
%x Used for lowercase hexadecimal number printing
%X Used for uppercase hexadecimal number printing
Escape Sequence Character in Python
An escape sequence is a sequence of characters that does not represent itself when
used inside a character or string literal, but is translated into another character or a
sequence of characters that may be difficult or impossible to represent directly

Escape Sequence Character Description


\n Insert a linefeed (new line)
\r Insert a carriage return
\f Insert a form feed
\b Insert a backspace
\t Insert a tab
\\ Insert a backslash
\" Insert a double quote
\' Insert a single quote
Frequently Asked Questions
1. What is Python Programming Language
2. History of Python
3. Applications of Python
4. Key Features of Python
5. Installation of Python on a Computer
6. How to use output function to print / write the values in Python
7. How to use input function to read the integer vales in python
8. How to do read(input ) and write (output) the string value in python
9. What are variables
10. What are Data
11. What are the different data types in python
12. Write a note on data types
13. What are different types of operators supported in python, explain with examples

You might also like