Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
76 views

Introduction To Python

The document discusses various Python concepts like variables, lists, tuples, data types, operators, loops, conditional statements, functions, arrays, matrices, and taking screenshots using Python. It provides examples and explanations of key terms like variables and data types. List methods like append and remove are described. Tuples are defined as immutable sequences. Mathematical operators and taking numerical input from the user are also covered at a high level.

Uploaded by

Gaurav garg
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Introduction To Python

The document discusses various Python concepts like variables, lists, tuples, data types, operators, loops, conditional statements, functions, arrays, matrices, and taking screenshots using Python. It provides examples and explanations of key terms like variables and data types. List methods like append and remove are described. Tuples are defined as immutable sequences. Mathematical operators and taking numerical input from the user are also covered at a high level.

Uploaded by

Gaurav garg
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

P

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

– A data type - int, float, str, etc.

– Storage space sufficient for the type.


Lists
• Think of a list as a stack of cards, on which
your information is written
• The information stays in the order you place it
in until you modify that order
• Methods return a string or subset of the list or
modify the list to add or remove components
• Written as var[index], index refers to order
within set (think card number, starting at 0)
• You can step through lists as part of a loop
4
List Methods
• Adding to the List
– var[n] = object
• replaces n with object
– var.append(object)
• adds object to the end of the list
• Removing from the List
– var[n] = []
• empties contents of card, but preserves order
– var.remove(n)
• removes card at n

5
SN Function with Description

1 cmp(list1, list2) Compares elements of both lists.

2 len(list) Gives the total length of the list.

3 max(list) Returns item from the list with max value.

4 min(list) Returns item from the list with min value.

5 list(seq) Converts a tuple into list.


32
Tuples:
A tuple is a sequence of immutable Python objects. Tuples are
sequences, just like lists. The differences between tuples and
lists are, the tuples cannot be changed unlike lists and tuples
use parentheses, whereas lists use square brackets.

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 +,-,*,/

Assignment operators =, +=, -=,*=

Relational operators >, <, ==, >=

Logical operators And(&),OR, Not


Math
Try typing this into Code:
>>> print 3 + 12
15
>>> print 12 – 3
9
>>> print 9 + 5 – 15 + 12
11

Operators:
add: +
subtract: -

Note: don’t type the arrows >>> ! 11


Math
Rule: If you want Python to answer in floats, you have to
talk to it in floats.

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

while loop Repeats a statement or group of statements


while a given condition is TRUE. It tests the
condition before executing the loop body.

for loop Executes a sequence of statements multiple


times and abbreviates the code that
manages the loop variable.

nested loops You can use one or more loop inside any
another while, for or do..while loop.
Statement Description

if statements An if statement consists of a boolean expression


followed by one or more statements.

if...else statements An if statement can be followed by an


optional else statement, which executes when
the boolean expression is FALSE.

nested if statements You can use one if or else if statement inside


another if or else if statement(s).
Break ,Continue and Pass
• Break = to jump out of the entire loop
• Continue = it will skip that value
• Pass = if you want function to be empty
Break, continue
Use “break” to stop the for loop
Use “continue” to stop processing the current item

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

•Install the pyautogui package


•Capture the path to save the screenshot
•Take the screenshot using Python
Python code for taking screenshot
• import pyautogui
• import tkinter as tk
• root= tk.Tk()
• canvas1 = tk.Canvas(root, width = 300, height = 300)
• canvas1.pack()

• def takeScreenshot ():


• myScreenshot = pyautogui.screenshot()
• myScreenshot.save(r'C:\Users\Ron\Desktop\Test\screenshot2.png')
• myButton = tk.Button(text='Take Screenshot', command=takeScreenshot,
bg='green',fg='white',font= 10)
• canvas1.create_window(150, 150, window=myButton)
• root.mainloop()

You might also like