Introduction To Python
Introduction To Python
Y
T
H
O
N
Submitted by:
Gaurav garg(101708062)
Content:
• Variables
• Lists
• Strings
• Tuples
• Data types
• Operators
• Loops and conditional statements
• Break ,Pass and Continue
• Printing Patterns
• Array
• Numpy
• Matrix
• Taking screenshot using python
Variables in Python
• A variable has
– A name – identifier
5
SN Function with Description
Example:
tup2 = (1, 2, 3, 4, 5 );
tup3 = ("a", "b", "c", "d“);
Accessing Values:
print "tup2[1:5]: “
Output:
8
tup2[1:5]: [2, 3, 4, 5]
Data Types
• None: when we not give ant value.
• Numeric: int,float,compex,bool.
• Sequence: list, tuple, set, string, range.
• Dictionary: here we will assign some key to
object.
Operators
Arithemetic operators +,-,*,/
Operators:
add: +
subtract: -
More operators:
divide: /
multiply: *
>>> print 3 * 12 36
>>> print 12 / 3 4
>>> print 11 / 3 3
>>> print 12.0 / 3.0 4.0
>>> print 11.0 / 3.0 3.66 12
Swap 2 variables
Logic:
1st method:
temp=a
a=b
b=temp
2nd method:
a,b=b,a
Import Math Functions
• Syntax:
import math
• Example:
X=math.sqrt(25)
Print(x) (Ans=5)
Numerical Input
• To get numerical input from the user, we use an
assignment statement of the form
<variable> = input(<prompt>)
• Here
– <prompt> would be replaced by a prompt for the user
inside quotation marks
– If there is no prompt, the parentheses are still needed
• Semantics
– The prompt will be displayed
– User enters number
– Value entered is stored as the value of the variable
LOOPS & CONDITIONAL STATEMENTS
Loop Type Description
nested loops You can use one or more loop inside any
another while, for or do..while loop.
Statement Description
Checking 3
>>> for value in [3, 1, 4, 1, 5, 9, 2]: The square is 9
... print "Checking", value Checking 1
... if value > 8: Ignoring
... print "Exiting for loop" Checking 4
... break The square is 16
Checking 1
... elif value < 3: Ignoring
... print "Ignoring" Checking 5
... continue The square is 25
... print "The square is", value**2 Checking 9
... Exiting for loop
>>>
Printing Patterns
Array in Python
• Use the len() method to return the length of
an array .
• You refer to an array element by referring to
the index number.
• You can use the append() method to add an
element to an array.
• You can use the pop() method to remove an
element from the array.
• You can also use the remove() method to
remove an element from the array.
Numpy
• NumPy is a package for scientific computing
which has support for a powerful N-
dimensional array object.
• NumPy provides multidimensional array of
numbers (which is actually an object).
How to create a NumPy array?
Working with Matrix
Addition of Two Matrices
Multiplication of Two Matrices
Access matrix elements, rows and
columns
A basic code for matrix input from user
Taking screenshot using python
Steps to Take a Screenshot using Python