PYTHON Prof. Muhammad Saeed.

Slides:



Advertisements
Similar presentations
Python for Science Shane Grigsby. What is python? Why python? Interpreted, object oriented language Free and open source Focus is on readability Fast.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Python Crash Course Containers 3 rd year Bachelors V1.0 dd Hour 3.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Scientific Computing with NumPy & SciPy NumPy Installation and Documentation  Not much on the home page—don’t buy the guide, it’s.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Built-in Data Structures in Python An Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Scientific Python Numpy Linear Algebra Matrices Scipy Signal Processing Optimization IPython Interactive Console Matplotlib Plotting Sympy Symbolic Math.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Introduction to python programming
Introduction to python
G. Pullaiah College of Engineering and Technology
Fundamentals of Programming I Overview of Programming
PH2150 Scientific Computing Skills
ECE 1304 Introduction to Electrical and Computer Engineering
Topics Designing a Program Input, Processing, and Output
10 - Python Dictionary John R. Woodward.
Python Variable Types.
Programming in R Intro, data and programming structures
Numpy (Numerical Python)
Introduction to Python
Containers and Lists CIS 40 – Introduction to Programming in Python
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
CS 100: Roadmap to Computing
Intro To Pete Alonzi University of Virginia Library
IPYTHON AND MATPLOTLIB Python for computational science
Variables, Expressions, and IO
Lecture 10 Data Collections
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
MATLAB DENC 2533 ECADD LAB 9.
Python Data Types Expressions, Variables, and Assignments Strings
PH2150 Scientific Computing Skills
PYTHON Prof. Muhammad Saeed.
Python Review.
Numerical Computing in Python
Introduction to Python
Introduction to Python
CS 100: Roadmap to Computing
Numpy (Numerical Python)
Numpy (Numerical Python)
Communication and Coding Theory Lab(CS491)
Python-NumPy Tutorial
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
PYTHON Prof. Muhammad Saeed.
Introduction to Matlab
PYTHON Prof. Muhammad Saeed.
Python Primer 1: Types and Operators
Introduction to Programming with Python
Python Basics Wen-Yen Hsu, Hsiao-Lung Chan Dept Electrical Engineering
Topics Designing a Program Input, Processing, and Output
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Topics Designing a Program Input, Processing, and Output
Dr. Sampath Jayarathna Cal Poly Pomona
Dr. Sampath Jayarathna Old Dominion University
CS 100: Roadmap to Computing
Class code for pythonroom.com cchsp2cs
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
An Introduction to Data Science using Python
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

PYTHON Prof. Muhammad Saeed

Python Dept. Of Comp. Sc. & IT, FUUAST Introduction Built-in Data Types Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Introduction to Python Projects: A project contains packages and modules Core Packages: numpy scipy matplotlib A package is a namespace containing modules or other packages. Modules: A module can contain executable statements as well as function definitions. Editor Spyder and jupyter notebook idle many more Interactive Shell (Python Command Prompt: ) >>> ( It is removed in Spyder version 3.6.2. can be used in ‘idle’ ) Interactive Shell (IPython Command Prompt: ) In [1]: Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Python Language (Basic Syntax) It is case-sensitive. It is object-oriented. Every identifier is an object. Packages and Modules are imported into program as: import numpy import numpy as np from scipy import signal, stats, sin, cos, exp from panda import * # ………….. is single line comment and “”” …………………. ……………………. “”” is a multiline comment For I/O, input and print functions are used n=input(‘\nEnter a number’) print(‘square of ‘,n,’ is equal to ’, n**2) Double quotes (“ “) or single quotes (‘ ‘) are used for strings at one’s free will. Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Operators Arithmatic = '+ ', '- ', '* ', '/ ', '// ', '% ', '** ' Comparison = '< ', '> ', '== ', '!= ', '<> ', '<= ', '>=' Assignment = '+= ','-= ','*= ','/= ','//= ','%= ','**= ' Bitwise = '| ', '& ', '^ ', '~ ', '<< ', '>> ‘ Logical = 'and', 'or', 'not’ Membership = ‘in’, ‘not in’ Identity = ‘is’, ‘is not’ Multiple Assignment: a, b, c = 3, -4.5, ’FUUAST’ Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Interactive Shell (Command Prompt ) Python Console We can type code directly into a running Python session >>> 5*4 20 >>> x=15 >>> x 15 >>> name = “Saif" >>> name ‘Saif' >>> print( "Hello", name) Hello Saif Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Interactive Shell (Command Prompt ) IPython Console In [5]: import matplotlib.pyplot as plt In [6]: import numpy as np In [7]: x = np.arange(-2*np.pi, 2*np.pi, 0.1) In [8]: y = np.sin(x) In [9]: plt.plot(x,y) Out [10]: [<matplotlib.lines.Line2D at 0x2214353eef0>] In [11]: %matplotlib qt5 ; In [45]: %matplotlib inline Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Built-in Data Types Numeric: int , float, complex String: List Tuple Set Dictionary Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Built-in Data Types Numeric: int , float, complex Initialization: a=150 c=10.76 Complex Numbers: z=complex(3,5) z=3+5j ; z1=3+1j ; z2=2-4j Methods: z.real ; z.imag ; z.conjugate ; abs(z) z=z1+z2 ; z=z1*z2 ; z=z1/z2 Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Built-in Data Types Strings: Initialization: Name1=“Python”; Name2=“Cython” ; Year=“2017” Concatenation, Splitting and repetition: Name3=Name1+” “+”with ”+Name2 Name1*4 s1="abc xyz uvw".split() Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Built-in Data Types Lists[]: Lists contain compound data types. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator. List index starts with 0. Initialization: Lst1=[4, 8, “Hello”, 4.5, -10.2, “Hi”, ‘A’, ‘B’] Lst2= [ 2, 4, 6, 10, 12, 18] Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Built-in Data Types Lists[]: Lst0=[4, 8,8,8, “Hello”, 4.5, -10.2,’Hi’] Lst1= [ 2, 4, 6, 10, 12, 18] Methods on Lists: Lst1.append(67) Lst1.insert(2, -10) Lst1.pop(index) Lst1.pop() Lst1.clear() Lst0.remove(4.5) Lst0.index(“Hi”) Lst0.count(y); y=8 Lst0.sort() Lst0.reverse() Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Built-in Data Types Tuple(): A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses. Tuples cannot be updated. They are read-only. Initialization: t1=(1,9,4,5) ; t2=(3, -8, “AOA”) ; t3=7, t4=tuple([1,2,3,4]) t5=((1,2,3),(4,5,6),(7,8,9),(10,11,12)) t=([3,6,0],[2,5]) Operations: p = t1, t2, t3 q = t1 + t2 + t3 p[1][2] Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Built-in Data Types Sets: {} Initialization: st1 = {1,4,-6,”Sukkur”,4} st2 = set([‘Pakistan’, ‘Russia’, ‘Iran’, ‘Afghanistan’, ‘Bangla Desh’]) st3 = set([‘Pakistan’, ‘Russia’, ‘Iran’, ‘Afghanistan’]) Methods on Sets: st2.add(‘China’) st2.union(st3) ; st2.intersection(st3) st2.difference(st3) ; st2 – st3 st3.remove(‘Afghanistan’) st2.issuperset(st3) ; st3.issubset(st2) st3.isdisjoint(st2) st1.clear() st5=frozenset([‘Pakistan’, ‘Russia’, ‘Iran’, ‘Afghanistan’]) st2.pop() Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Built-in Data Types Dictionary{}: Python's dictionaries are kind of hash table type. They work like associative arrays and consist of key-value pairs. Dictionaries have no concept of order among elements. Initialization: dict1 = {'Name': 'Zara', 'Age': 17, 'Class': 'First Semester'} dict1[‘Name’]=‘Zehra’ dict1[‘Roll No’]=25 dict2={} Methods on Dictionary: dict1.keys() ; dict1.values(); dict1.items() len(dict1) del(dict1[‘Name’]) dict2.update(dict1) dict2.clear() dictt=zip(list1,list2) ; dict(dictt) Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Arrays & Matrices Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT FUUAST Arrays: index starts with 0 import numpy as np Initialization: a1 = np.array([1, 2, 3, 4, 5, 6]) a2 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) a3 = np.array([1, 2, 3, 4, 5, 6],np.int64) x=np.arange(20); v=np.arange(0, 5,0.1) z=np.linspace(-5, 5, 21) Built-in Array Creating Functions: np.zeros((3, 4)) ; np.zeros((3, 4), dtype=np.float) np.ones((3, 2)); np.eye(3,3) np.diag(a2) Determinant: np.linalg.det(a1) Python Dept. Of Comp. Sc. & IT FUUAST

Python Dept. Of Comp. Sc. & IT FUUAST Arrays: import numpy as np a = np.array([1,2,3,4,5,6,7,8,9,10,11,12]) a2 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) a3= np.array([[0,0,0,0],[1,1,1,1],[2,2,2,2]]) a4=np.random.rand(3,2); a5=np.random.randint(0, 20, 15) Operaions on Arrays: y=a2.T; a2+3; a2*5; a4=[[0]*10] a.clip(4,9); np.split(a,4) Stacking a2.flatten(); np.ravel(a2) a4= np.stack((a2,a3),1); a5= np.hstack((a2,a3)); a6= np.vstack((a2,a3)); Python Dept. Of Comp. Sc. & IT FUUAST

Python Dept. Of Comp. Sc. & IT FUUAST Matrices: How to create Matrices: m=np.array([1,2,3,5,4,8,0,1,7]).reshape(3,3) mat1=np.matrix(m); mat1=np.mat(m) mat3=np.matrix([[2, 0, 1],[ 1, 4, 3],[5, 2, 0]]) mat4=mat3+mat3*1.j; mat4.imag; mat4.real mat5=np.complex(mat3) Operations on Matrices: mat3.I #Inverse mat3.T #Transpose mat3.H #Hermitian Transpose mat3.A #Array Object mat3.A1 #Flattened Array mat1+mat3; mat1+5 mat1*mat3; mat1*5 mat1**3 Python Dept. Of Comp. Sc. & IT FUUAST

Python Dept. Of Comp. Sc. & IT FUUAST Matrices: More Operations and Linear Algebra: mat1=np.matrix([[2, 0, 1],[ 1, 4, 6],[5, 2, 0]]) mat1.ndim np.linalg.matrix_rank(mat1) mat1.clip(min, max) Determinant, Rank, Eigen Values and Eigen Vectors np.linalg.det(mat1) np.linalg.matrix_rank(mat1); w,v=np.linalg.eig(mat1) Python Dept. Of Comp. Sc. & IT FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Data Types & Common Operations. I Slicing & Manipulation: a1[2]; a2[1,2]; a1[:]; a1[:-2]; a4 = a2; a2[:,1]; a2[0:2,1:3]; a1[-2:]; a2[::2] m1[1,2]=6 d=np.diag(m); np.diag(d) Information: type(a1); type(a2[0,2]); a2.dtype a2.size; len(a2); a2.shape Type Casting: int(Year) ; float(Year); complex(Year) x=450; s=str(x) Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Data Types & Common Operations. 2 Type Casting: np.int32(y) np.int64(y) np.float64(y) complex(y) x=450; s=str(x) Type Query: type(a) Deletion of identifiers from memory: del a del z, c %reset Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Data Types & Common Operations. 3 import numpy as np a2 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) a1=np.array([13,14,15,16]) Mathematical and Statistical Methods: a2.sum(); a2.sum(axis=0); a2.sum(axis=1) a5=a2.max() ; a6=a2.min(1); a2.ptp(0) a2.mean(0) ; a2.std(1) ; a2.var(); np.median(a2); np.median(a2,0); np.median(a2,1) x=scipy.stats.mode(a) x=np.cov(a) np.random.rand(3,2); np.random.randint(0, 20, 15) a3=np.sin(z);…… cos, exp, log, log10, sqrt, etc. Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT FUUAST Data Types & Common Operations. 4 Truncation Functions: np.floor(2.84) np.floor(-2.84) np.floor([1.45, 2.987, 3.12]) np.ceil(3.246); np.ceil(-3.246) scipy.round_(12.3456732, 4) np.around([12.3456732], decimals=4) np.fix(3.65) np.fix(-3.65) np.trunc(6.356) np.trunc(-6.356) Python Dept. Of Comp. Sc. & IT FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST Data Types & Common Operations. 4 Constants: np.nan # NaN, NAN np.inf -np.inf np.isnan(a) np.isneginf(a); np.isposinf(a) np.isfinite(a) Input/Output: x=input(‘Enter a number: ‘) print(‘your recent facebook post is very informative’) print(‘Square of ‘, a, ’=‘, a**2) print(‘Square root of %d = %12.5f’%(a, np.sqrt(a)) Python Dept. Of Comp. Sc. & IT, FUUAST

Python Dept. Of Comp. Sc. & IT, FUUAST END Python Dept. Of Comp. Sc. & IT, FUUAST