0% found this document useful (0 votes)
15 views11 pages

pythonCW 86525

This document provides an introduction to Python, outlining its features, applications, advantages, and installation methods. It covers basic programming concepts such as data types, operators, conditional statements, loops, lists, tuples, and strings, along with sample code examples. Additionally, it discusses the importance of algorithms and flowcharts in program design.

Uploaded by

krishnan.bkkh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

pythonCW 86525

This document provides an introduction to Python, outlining its features, applications, advantages, and installation methods. It covers basic programming concepts such as data types, operators, conditional statements, loops, lists, tuples, and strings, along with sample code examples. Additionally, it discusses the importance of algorithms and flowcharts in program design.

Uploaded by

krishnan.bkkh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit 3: Introduction to Python.

Python is a popular programming language. It


was created by Guido van Rossum, and
released in 1991.
Features of python
1. Easy to code: Python is a high-level
programming language which is easy to
understand , learn compared to other
languages like Java, C++ etc., It is also a
developer-friendly language.
2. Free and Open source: Python
language is freely available at the official
website and can download. Since it is
open source, means the source code is
also available to the public.
3. Object-oriented language: Python
supports object oriented programming
concepts of classes, objects etc.,
4. Portable language: Python is a
portable language which can run in other
platforms such as Linux, Unix and
Macintosh etc., It has the same interface
on all platforms.
5. Large standard library: Python has a
large standard library which provides rich
set of module and functions which helps
the user to execute the code.
6. Dynamically typed language: Python is
a dynamically typed language which
means the data type for a variable is
decided at run time and not in advance.

Applications of Python
 Python can be used on a server to create
web applications.
 Python can connect to database systems.
It can also read and modify files.
 Python can be used to handle big data and
perform complex mathematics.
 Python can be used for software
development.
Advantages of Python
 Python works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc).
 Python has a simple syntax similar to the English
language.
 Python has syntax that allows developers to write
programs with fewer lines than some other
programming languages.
 Python runs on an interpreter system, meaning that
code can be executed as soon as it is written.
Installation of Python
Anaconda Package Installation:

 Log on to https://www.anaconda.com/distribution.in

Restrictions of python:
1. Spaces or indentation should be carefully used in Python.
The amount of indentation matters.A missing or extra space in
a Python block could cause an error.
2. Python Variables are case sensitive.
3. "Run" in Jupyter Notebook or spyder is used to execute the
python code .

Modes of Python :
There are two ways of working with python. They
are:
1. Interactive mode: This mode allows the users
to interact with Operating system. Since python
is an interpreter language, in interactive mode
steps of execution are given one by one.
2. Script mode : Python scripts are program
instructions written as a sequence of steps and
end in a file extension of (.py), here the python
program typed in a file and then an interpreter
is used to execute the content from the file.
Basic functions of python:
print( ) : print function is used to print a
statement on a screen.
Example : print(‘hello! world’)

input( ): input function is used to take


input from the users.

Example: L=int(input("Enter the length"))

Example program :
s=int(input("marks")) 90.0
B=int(input("age")) 14
print(“marks of a student:”,s)
print(“age of a student:”, B)
Python Comments: A comment is a non- executable
text that doesn't affect the outcome of a code. In
Python, we use the hash (#) symbol to start writing a
comment.
Example: # code to execute area of circle

Keywords
Keywords are the reserved words in Python used by
interpreter to recognize the structure of the program.
Example: for, while, if, elif , else, print, input
Variables :A variable is a memory location which
stores a value. Variables act as a container that holds
data which can be changed later throughout
programming.
Example : int a=50; # here ‘a’ is a
variable.

Data Types : In Python, each value has a


datatype. Python supports several data
types, including integers, floats, strings,
and more.
int float string

age = 25 temperature = 98.6 name = "Alice"

Operators : Operators are symbolic


representations of computation. They are used
with operands, which can be either values or
variables.
 Arithmetic operators which performs
arithmetic operations in a program such as
+,-,*,/,% etc.,

 Assignment operator is used in Python to


assign values to variables. For example, a = 5
is a simple assignment operator that assigns
the value 5 on the right to the variable a on
the left.

Relational operators
Equal to == Not equal to !=
Less than < Less than equal to
<=
Greater than > Greater than equal to
>=

Logical operators
AND, OR, NOT.

PROGRAM DESIGN TOOLS


Algorithm and Flowchart
Algorithm is a step-by step process used to
solve a given problem.
Example: Algorithm to find the sum of two
numbers.
Step 1: Start
Step 2: Read A,B,Sum
Step 3: Sum=A+B
Step 4: Print Sum
Step 5: Stop.
Flowchart is a pictorial representation of an
algorithm to solve a given problem.
Flowchart symbols
- Start/Stop

- Processing

- Input/Output

- condition

- sequence

Example: Flowchart to find addition of two


numbers.
Sample python programs:

Code 1: Python code for basic arithmetic


operations
x = 10 #variable x
y=5 #variable y
addition = x + y
subtraction = x - y
multiplication = x * y
division = x / y # returns quotient
remainder=x%y #returns remainder
print(“Total is”,addition)
print(“Difference is”,subtraction)
print(“Product is”,multiplication)
print(“Quotient is”,division)
print(“remainder is”,remainder)

Code 2: Python code to calculate Area


and Perimeter of a rectangle
L=int(input("Length :")) #taking user input
for length.
B=int(input("Breadth :")) #taking user input
for breadth.
Area=L*B
Perimeter=2*(L+B)
print("Area:",Area) #print area
print("Perimeter:",Perimeter) #print
perimeter
Conditional Statements
Conditional statement is used to check a particular condition and then
execute the code only if the specified condition is satisfied.

if-else condition :
In the if else, if the condition is true, the statements inside the if
block will execute and if the condition is false the statements in
the else block will execute.
Syntax: if (condition 1):
Statement 1
else:
Statement 2

Example: Python code using if-else


conditional statement.
age=int(input(“enter the age of a person”))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")

Iterative/ Looping statements


Looping statement: Execution of the statements
repeatedly until the condition returns false.
 The for Statement:The for loop allows you to
repeatedly execute a block of statements as long as a
condition is true.
Syntax:
for variable in range:
Body of for
Here, "in range" means how many times the instructions in the
loop will be executed.
Example Code : Python code using for loop to print the value of a
variable
for i in range(10) :
print(i)

 The while Statement:


The while statement allows you to repeatedly execute a block
of statements as long as a condition is true.

Syntax :
while test_expression:
Body of while

Example : Program to add natural # numbers upto # sum =


1+2+3+...+n

n = int(input("Enter the value of n: "))


sum = 0
i=1
while i <= n:
sum = sum + i
i = i+1
print("The sum is", sum)

When you run the program, the output will be:


Enter n: 10
The sum is 55

Lists
A List is a sequence datatype used to store multiple
data in a single variable.
Each element or value that is inside of a list is called
an item.
 Lists are defined by having items between square
brackets [ ] and identified by an index number .
Example: Desserts=["Vanilla", "Chocolate", "Strawberry", "Black
Currant"]

In the above example, 4 items are in the list Desserts where


each item is stored in address,

Index 0 Index 1 Index 2 Index 3


Vanilla Chocolate Strawberry Black currant

Sample codes on Lists

Code : Python code to print an element from a list

Lunch=["pizza","pasta","hamburger","cookies","sandwi
ch"]
print(Lunch[2])

Code : Python code using for loop to print the


elements of a list

Desserts=["Vanilla", "Chocolate", "Strawberry","Black


Currant", "Butterscotch"]
for x in range(5):
print(Desserts[x])

Tuples
Tuple is a sequence data type which is ordered and
unchangeable.
The sequence of values stored in a tuple can be of
any type, and they are indexed by integers.
Values of a tuple are syntactically separated by
‘commas’.
Example:
fruits = ("apple", "banana", "cherry")
Index 0 Index 1 Index 2
Apple Banana cherry

Sample Codes:
fruits = ("apple", "banana", "cherry")
print(fruits[1])
The above code will give the output as "banana"

Strings
Strings are set of characters surrounded by
single(‘ ‘) or double (“ “) quotation marks.
Example: ‘Hello! How are you.’
“ My name is RamKumar.”

Example program to print multiline strings.


a=”””Hello! How are you?
My Name is Ramkumar.
My Address is 25, Jangpura Lane, New Delhi.”””
print (a)

*****************

You might also like