Introduction To Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

INTRODUCTION TO

PYTHON
Algorithms
An algorithm is a step-by-step method for solving a problem
within a specific amount of time.
Characteristics of Algorithms :-
✔ It is simple to grasp.
✔ An algorithm has step-by-step instructions, it is free from any
programming code.
✔An algorithm has well defined inputs.
✔ An algorithm terminates after finite number of steps.
Flowcharts
A flowchart is a type of diagram that represents an algorithm, workflow
or process. Each symbol of a flowchart represents something. We can’t
replace to each other.
Process symbol Decision Box

Terminator or Input / Output Box


Start/End Box
Characteristic of Flowcharts:-
✔ It is a more effective technique of explaining the logic of the
system.
✔ Problem can be studied more efficiently.
✔ It is quite helpful throughout the program development phase.
✔ Using a flowchart makes it simple to maintain running
programs.
Introduction to programming

Computer programming is the process of designing and


giving a set of instructions to a computer so that it can
perform specific predefined tasks. Example : C, C++,
Java, Python, etc
What is Program?

A Computer program is a set of instructions written in a


computer programming language that instructs the computer
to accomplish a certain task. It’s also known as a computer
software.
INPUT PROCESS OUTPUT

Python is a simple and high level programming


language. It used to write codes for designing artificial
system (AI). Python was created by Guido Van
Rossum in 1991.
USE OF PYTHON
FEATURES OF PYTHON
PROGRAMMING
• It is an easy to learn general purpose • Build a Website
high-level programming language.
• Develop Games
• It is a platform independent programming
• Program Robots
language. (Any machine and any OS)
• Develop AI (Artificial
• It has simple syntax.
Intelligence) Application base
• Python is a case-sensitive language.
• Mathematical Applications.
• It is free to use and open source.
• System scripting
• Can interact with databases.
• It is capable of handling big data.
TO INSTALL PYTHON
It is free, so we can download https://www.python.org/ and install it. After
installing python, we can start with any of the following two modes:
❖ Interactive Mode
❖ Script Mode
Go To Start Or Search -> IDLE (Python 3.10 64-bit). (Integrated Development and
Learning Environment)
Or
We can use an online ( Integrated Development Environment) IDE, repl.it. This IDE can
be accessed at https://repl.it/
Title Bar
Menu Bar

Command Prompt
Statements
Any line of code that we write in a program is called statement. A set
of statement help us to get the required output.
Simple Statements:- A simple statement is comprised within a single
logical line.
Multi-line Statements:- Those statements which get extended to more
than one line but they still need to be read by an interpreter as one
single statement. For this we use parentheses( ), braces{}, square
brackets[ ], semi-colon (;), continuation character slash (\).
Inbuilt Function print( )

Python’s inbuilt function print( ) is used for printing the information on


the screen.
The syntax for the print( )is:
print (Message/Value)
print(“WELCOME TO PYTHON WORLD”)
Output: WELCOME TO PYTHON WORLD
Note: As we know python is case sensitive so in print command we
can’t put “P” (cap) in place of “p”.
We can type arithmetic expression at the python command prompt without print( )
function.
>>> 10 + 20
OUT PUT 30
>>> 45 / 6
OUT PUT 7.5
>>> 10 * 9
OUT PUT 90
>>>
COMMENTS
Comments are used in programs for explaining the code. Comments in Python
are identified with a hash symbol, #, and extend to the end of the line. Anything written
after # will be ignored by python.
The syntax for writing single-line comments in Python is:
# statement
Example: # This is comment
For writing a multi-line comment, add # before every line or enclose multiple lines within
triple quotes.
The syntax for writing multi-line comments in Python is:
”””
This is comment
”””
KEYWORDS AND IDENTIFIERS
Keywords:- A python keywords is a reserved word that cannot be used to name a variable,
class, function, or anything else. Example – false, none, true, return, del, elif, break etc.
Identifiers:- Identifiers in Python are words that we use to specify or declare variables. It
is case sensitive.
❑ A Identifier name must start with alphabet or underscore.
❑ A Identifier name can consist of letters, digits and underscore. No, other character is
allowed.
❑ A keyword can’t be use as a Identifier name (print, end, input, etc.)
❑ It can be any length but it’s case-sensitive.
VARIABLES
When we are working with the values in python, we require some storage location to hold
the values for later use named storage locations in the computer memory, which are used
to store data are called variables. A variable can store only one data value at a time.
Exp:- a = 10 # value 10 is assigned to the variable a
b = 20 # value 20 is assigned to the variable b
c=a+b # the numbers are added and the value is assigned to the variable c,
i.e.. 30 is assigned to the variable c.
Note: This is example of single value to single variables
Python allows us to assign values to multiple variables in one line.
Exp: x, y, z = “Hello”, “World”, 5
Output : Hello
World
5
Rules for Naming Variables in Python
❑ A variable name must start with alphabet or underscore.
❑ A variable name can consist of letters, digits and underscore. No, other character is
allowed.
❑ A keyword can’t be use as a variable name (print, end, input, etc.)
❑ It can be any length but it’s case-sensitive.
DATA TYPES
In python, a data type represents the type of data stored in a variable. There are three
basic data types:
int (integer): - It represent integral numbers (numbers without any fractional part).
Example: 3, 43, 15, -34, 2345, -3456
Note: 1. There is restriction on the length of int data type but it is depends on system
memory on which python is running.
2. We can find out the data type with inbuilt function type( )
Example: x = 5
print(type(x))
Output: <class ‘int’>
float :- It represents floating point values (numbers with fractional part) and the
numbers in scientific notation, i.e., with e or E. It can take both negative and
positive values.
Example: 23.43, 34.00, - 432.34, 4e7, 56E10, etc.
str (string): - String data type represents strings of characters enclosed with single
or double quotation mark (‘ ’ or “ ” ).
Note:- We cannot conduct mathematical operations on the str data type.

Operators
Operators are special characters that represent computation. It can be
either values or variables, it is depending on the data type.
ARITHMETIC OPERATORS
Arithmetic operators are used in Python for performing mathematical operations on
variables and values. Python has seven arithmetic operators.

Operator Name Example Comment


+ Addition x+y Will return the sum of the values
- Subtraction x-y Will return the difference of the values
* Multiplication x*y Will return the product of the values
/ Division x/y Will return the quotient of the division
% Modulus x%y Will return the remainder of the division
** Exponention x ** y Will return the exponential of the value
// Floor division x // y Will return the integral quotient of the division
For example –
x =10
y=2
z=3
a=x+y+z
print(a) # will give value 15
a=x+2
print(a) # will give value 12
a = x // z
print(a) # the value will be 3 (the integral portion of the quotient)
Relational operators
Relational operators are also known as comparison operators. They are used to
compare values and return either True or False.
Logical operators
There are three logical operators, and, or, not

Assignment operators
Assignment operator is used in python to assign values to variables.
STATEMENTS AND EXPRESSIONS
Statements and expressions are two different things in python. Statements are unit
of code which the interpreter can execute. There are a combination of values,
variables, functions and/or operators which the interpreter has to evaluate before
execution.
Example: x =2 # This is a statement. In this, the interpreter assigns value 2 to
variable x. This is no evaluation.
y =3 # This is also a statement.
d = x+y # This is an expression. The interpreter has to evaluate x+y before
execution, i.e. assignment to variable d.
print(x+y) # This is also an expression.
INBUILT FUNCTION INPUT( )
The input ( ) function is used to accept the value for a variable from the user. We
can use int( ) or float( ) along with input( ) function.
Example: print (‘Enter Your Name:’)
x=input( )
print (‘Hello’)
print(x)
Output:- Enter Your Name: Tom
Hello
Tom
By default, the input taken by the input( ) function will be of str type. It does not
help us if we want to use it for calculation.
x=input(‘Enter any Number:’)
y=x*6
print(y)
Output:- Enter any Number: 56
565656565656
In this the operation of ‘*’ will also be conducted on a string. For Solving this
problem we have to tell the interpreter to accept the input as int, by usng int( ).
x=int(input(‘Enter any Number:’))
y=x*6
print(y)
Output: Enter any Number: 56
336
a =int( input(“Enter the first value:”,))
Enter the first value: 100
b =int( input(“Enter the second value:”,))
Enter the second value: 200
print(“The sum of the two values :”,a+b)
MORE ON PRINT( )
The Python print( ) function can be used for more complex output.
We can use print( ) function for combining text and variables. The “+” character is
used for combining variables and text in print( ) function.
Example: x = “boy.”
print(“I am a ” + x) Output: I am a boy.

x = “I am a ”
y = “Girl.”
z = x+y
print (z) Output : I am a Girl.
Python does not allow the use of ‘+’ character for combining a string and a
number.
Example: x = 5
y = “Hello”
print(x+y) # This line will give error.
For solve this problem we can be done by using the str( ) function.
x=5 x = “5”
y = “ Hello” Or y = “Hello”
print(str(x) + y) print(x+y)

Output : 5 Hello
EXERCISE-1
Create a program that asks for your Name and Age, and then displays this on the screen.
Name=input("Enter your Name: ")
Age=input("How old you are: ")
print("Your Name is "+Name)
print("Your are "+Age+" years Old")
Create a program that accepts a number and then prints a table for that number.
n=int(input("Please Enter a Number:"))
a=n*1
print(str(n)+" X 2 = "+str(a))
a=n*2
print(str(n)+" X 2 = "+str(a))
a=n*3
print(str(n)+" X 2 = "+str(a))
a=n*4
print(str(n)+" X 2 = "+str(a))
a=n*5
print(str(n)+" X 2 = "+str(a))
a=n*6
print(str(n)+" X 2 = "+str(a))
Continue …….
Python have powerful feature of inbuilt data structures or collections. There
are four type of collections: -
List:- An ordered and changeable collection that allows duplicate members.
Tuple:- An ordered and unchangeable collection that allows duplicate
members.
Set:- An unordered and unindexed collection that does not allow duplicate
members.
Dictionary:- An unordered, changeable, and indexed collection that does not
allow duplicate members.
LISTS
Creating lists in Python are created using square brackets.[ ]
List1 =[ 21,34,34,54,51]
List2 =[“Jan”, “Feb”, “Mar”, “Apr”, “May”,]
List3 =[ 18, 7, 2, “Oct”, “Nov”, “Dec”]
Printing Lists
print (List1) # [ 21,34,34,54,51]
print (List2) # [“Jan”, “Feb”, “Mar”, “Apr”, “May”,]
print(List3) # [ 18, 7, 2, “Oct”, “Nov”, “Dec”]
ACCESSING INDIVIDUAL ITEMS ON THE LISTS
Each item of the list has an index number. Python allows both negative
and positive indexing. In positive indexing , the first item on the list
has an index number 0, and each item on the right has an index number
of +1.
List1=[7, 11, “Feb”, “Nov”]
print(List1[1]) # 11
print(List1[3]) # Nov
print(List1[-2]) # Feb
print(List1[-4]) # 7
RANGE OF INDEXES
We can also access the lists by specifying the range of index.
List1=[7, 11, 2, “Feb”, “Nov” “Dec”]
print(List1[1:4])
print(List1[-2:5])
CHANGING OUT THE NUMBER OF ITEMS IN THE LIST
The individual items in the list can be changed by using their index numbers.
List1=[“Jan”, “Feb”, “Mar”, “Apr”]
List1[2]=“Nov”
print(List1) #[“Jan”, “Feb”, “Nov”, “Apr”]

You might also like