Introduction To Python
Introduction To Python
Introduction To Python
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
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( )
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.
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”]