KEMBAR78
Basic Introduction to Python Programming | PPT
PYTHON
LANGUAGE
Dr. B. Subashini,
Assistant Professor of Computer Applications,
V.V.Vanniaperumal College for Women,
Virudhunagar
BRIEF HISTORY OF PYTHON
• Invented in the Netherlands, early 90s by
Guido van Rossum
• Named after Monty Python
• Open sourced from the beginning
• Considered a scripting language, but is much
more
• Scalable, object oriented and functional from
the beginning
• Used by Google from the beginning
• Increasingly popular
PYTHON
• Python can be used on a server to create web
applications.
• Python can be used alongside software to create
workflows.
• Python can connect to database systems. It can also read
and modify files.
• Python can be used to handle big data
and perform complex mathematics.
• Python can be used for rapid prototyping,
or for production-ready software development.
WHY PYTHON?
• Python works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc).
• Python has a simple syntax similar to the English
language.
• Python has syntax that allows developers to write
programs with fewer lines than some other programming
languages.
• Python runs on an interpreter system, meaning that
code can be executed as soon as it is written. This
means that prototyping can be very quick.
• Python can be treated in a procedural way,
an object-oriented way or a functional way.
VERSION
• The most recent major version of Python is
Python 3
• 3.12.0 is the latest version of Python released on
October 2, 2023
• However, Python 2 being updated with security,
is still quite popular
PYTHON SYNTAX
• Python was designed for readability, and has some
similarities to the English language with influence
from mathematics.
• Python uses new lines to complete a command, as
opposed to other programming languages which
often use semicolons or parentheses.
• Python relies on indentation, using whitespace, to
define scope; such as the scope of loops, functions
and classes. Other programming languages often
use curly-brackets for this purpose.
COMMENTS
• Comments can be used to explain Python code.
• Comments can be used to make the code more
readable.
• Comments can be used to prevent execution when
testing code.
• Example:
• #This is a comment
print("Hello, World!")
VARIABLES
• Variables are containers for storing data values.
• Python has no command for declaring a variable.
• A variable is created the moment just first assign a value
to it.
• x = 5
y = "John"
print(x)
print(y)
VARIABLE NAMES
• A variable name must start with a letter or the
underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and
AGE are three different variables)
• A variable name cannot be any of the Python
Keywords.
BUILT IN DATA TYPES
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
NUMPY
• Stands for Numerical Python
• Is the fundamental package required for high performance
computing and data analysis
• NumPy is so important for numerical computations in
Python is because it is designed for efficiency on large
arrays of data.
• It provides ndarray for creating multiple dimensional arrays.
• Internally stores data in a contiguous block of
memory, independent of other built-in
Python objects, use much less memory
than built-in Python sequences.
NUMPY NDARRAY VS LIST
• Standard math functions for fast operations on entire arrays of data
without having to write loops
• NumPy Arrays are important because they enable to express batch
operations on data without writing any for loops. We call this
vectorization.
• One of the key features of NumPy is its N-dimensional array object, or
ndarray, which is a fast, flexible container for large datasets in Python.
• Whenever you see “array,” “NumPy array,” or “ndarray” in the text, with
few exceptions they all refer to the same thing: the ndarray object.
• NumPy-based algorithms are generally 10 to
100 times faster (or more) than their pure Python
counterparts and use significantly less memory.
import numpy as np
my_arr = np.arange(1000000)
NDARRAY
• ndarray is used for storage of homogeneous data
• i.e., all elements the same type
• Every array must have a shape and a dtype
• Supports convenient slicing, indexing and efficient
vectorized computation
import numpy as np
data1 = [6, 7, 5, 8, 0, 1]
arr1 = np.array(data1)
print(arr1)
CREATING NDARRAYS
Using list of lists
import numpy as np
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]] #list of lists
arr2 = np.array(data2)
print(arr2.ndim) #2
print(arr2.shape) # (2,4)
array = np.array([[0,1,2],[2,3,4]])
[[0 1 2]
[2 3 4]]
array = np.zeros((2,3))
[[0. 0. 0.]
[0. 0. 0.]]
array = np.ones((2,3))
[[1. 1. 1.]
[1. 1. 1.]]
array = np.arange(0, 10, 2)
[0, 2, 4, 6, 8]
array = np.eye(3)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
array = np.random.randint(0,
10, (3,3))
[[6 4 3]
[1 5 6]
[9 8 5]]
CREATING NDARRAYS
ARITHMETIC WITH NUMPY
ARRAYS
• Any arithmetic operations between equal-size arrays
applies the operation element-wise:
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
[[1 2 3]
[4 5 6]]
print(arr * arr)
[[ 1 4 9]
[16 25 36]]
print(arr - arr)
[[0 0 0]
[0 0 0]]
ARITHMETIC WITH NUMPY
ARRAYS
• Arithmetic operations with scalars propagate the scalar
argument to each element in the array:
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
print(arr)
[[1. 2. 3.]
[4. 5. 6.]]
print(arr **2)
[[ 1. 4. 9.]
[16. 25. 36.]]
ARITHMETIC WITH NUMPY
ARRAYS
• Comparisons between arrays of the same size yield
boolean arrays:
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
print(arr)
[[1. 2. 3.]
[4. 5. 6.]]
arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])
print(arr2)
[[ 0. 4. 1.]
[ 7. 2. 12.]]
print(arr2 > arr)
[[False True False]
[ True False True]]
INDEXING AND SLICING
• One-dimensional arrays are simple; on the surface they act
similarly to Python lists:
arr = np.arange(10)
print(arr) # [0 1 2 3 4 5 6 7 8 9]
print(arr[5]) #5
print(arr[5:8]) #[5 6 7]
arr[5:8] = 12
print(arr) #[ 0 1 2 3 4 12 12 12 8 9]
INDEXING AND SLICING
• An important first distinction from Python’s built-in lists is
that array slices are views on the original array.
• This means that the data is not copied, and any modifications to
the view will be reflected in the source array.
arr = np.arange(10)
print(arr) # [0 1 2 3 4 5 6 7 8 9]
arr_slice = arr[5:8]
print(arr_slice) # [5 6 7]
arr_slice[1] = 12345
print(arr)
# [ 0 1 2 3 4 5 12345 7 8 9]
arr_slice[:] = 64
print(arr)
# [ 0 1 2 3 4 64 64 64 8 9]
INDEXING
• In a two-dimensional array, the elements at each index are
no longer scalars but rather one-dimensional arrays:
• Thus, individual elements can be accessed recursively.
But that is a bit too much work, so we can
pass a comma-separated list of indices
to select individual elements.
• So these are equivalent:
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr2d[2]) # [7 8 9]
print(arr2d[0][2]) # 3
print(arr2d[0, 2]) #3

Basic Introduction to Python Programming

  • 1.
    PYTHON LANGUAGE Dr. B. Subashini, AssistantProfessor of Computer Applications, V.V.Vanniaperumal College for Women, Virudhunagar
  • 2.
    BRIEF HISTORY OFPYTHON • Invented in the Netherlands, early 90s by Guido van Rossum • Named after Monty Python • Open sourced from the beginning • Considered a scripting language, but is much more • Scalable, object oriented and functional from the beginning • Used by Google from the beginning • Increasingly popular
  • 3.
    PYTHON • Python canbe used on a server to create web applications. • Python can be used alongside software to create workflows. • Python can connect to database systems. It can also read and modify files. • Python can be used to handle big data and perform complex mathematics. • Python can be used for rapid prototyping, or for production-ready software development.
  • 4.
    WHY PYTHON? • Pythonworks on different platforms (Windows, Mac, Linux, Raspberry Pi, etc). • Python has a simple syntax similar to the English language. • Python has syntax that allows developers to write programs with fewer lines than some other programming languages. • Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick. • Python can be treated in a procedural way, an object-oriented way or a functional way.
  • 5.
    VERSION • The mostrecent major version of Python is Python 3 • 3.12.0 is the latest version of Python released on October 2, 2023 • However, Python 2 being updated with security, is still quite popular
  • 6.
    PYTHON SYNTAX • Pythonwas designed for readability, and has some similarities to the English language with influence from mathematics. • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. • Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose.
  • 7.
    COMMENTS • Comments canbe used to explain Python code. • Comments can be used to make the code more readable. • Comments can be used to prevent execution when testing code. • Example: • #This is a comment print("Hello, World!")
  • 8.
    VARIABLES • Variables arecontainers for storing data values. • Python has no command for declaring a variable. • A variable is created the moment just first assign a value to it. • x = 5 y = "John" print(x) print(y)
  • 9.
    VARIABLE NAMES • Avariable name must start with a letter or the underscore character • A variable name cannot start with a number • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) • Variable names are case-sensitive (age, Age and AGE are three different variables) • A variable name cannot be any of the Python Keywords.
  • 10.
    BUILT IN DATATYPES Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary Types: bytes, bytearray, memoryview None Type: NoneType
  • 11.
    NUMPY • Stands forNumerical Python • Is the fundamental package required for high performance computing and data analysis • NumPy is so important for numerical computations in Python is because it is designed for efficiency on large arrays of data. • It provides ndarray for creating multiple dimensional arrays. • Internally stores data in a contiguous block of memory, independent of other built-in Python objects, use much less memory than built-in Python sequences.
  • 12.
    NUMPY NDARRAY VSLIST • Standard math functions for fast operations on entire arrays of data without having to write loops • NumPy Arrays are important because they enable to express batch operations on data without writing any for loops. We call this vectorization. • One of the key features of NumPy is its N-dimensional array object, or ndarray, which is a fast, flexible container for large datasets in Python. • Whenever you see “array,” “NumPy array,” or “ndarray” in the text, with few exceptions they all refer to the same thing: the ndarray object. • NumPy-based algorithms are generally 10 to 100 times faster (or more) than their pure Python counterparts and use significantly less memory. import numpy as np my_arr = np.arange(1000000)
  • 13.
    NDARRAY • ndarray isused for storage of homogeneous data • i.e., all elements the same type • Every array must have a shape and a dtype • Supports convenient slicing, indexing and efficient vectorized computation import numpy as np data1 = [6, 7, 5, 8, 0, 1] arr1 = np.array(data1) print(arr1)
  • 14.
    CREATING NDARRAYS Using listof lists import numpy as np data2 = [[1, 2, 3, 4], [5, 6, 7, 8]] #list of lists arr2 = np.array(data2) print(arr2.ndim) #2 print(arr2.shape) # (2,4)
  • 15.
    array = np.array([[0,1,2],[2,3,4]]) [[01 2] [2 3 4]] array = np.zeros((2,3)) [[0. 0. 0.] [0. 0. 0.]] array = np.ones((2,3)) [[1. 1. 1.] [1. 1. 1.]] array = np.arange(0, 10, 2) [0, 2, 4, 6, 8] array = np.eye(3) [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] array = np.random.randint(0, 10, (3,3)) [[6 4 3] [1 5 6] [9 8 5]] CREATING NDARRAYS
  • 16.
    ARITHMETIC WITH NUMPY ARRAYS •Any arithmetic operations between equal-size arrays applies the operation element-wise: arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) [[1 2 3] [4 5 6]] print(arr * arr) [[ 1 4 9] [16 25 36]] print(arr - arr) [[0 0 0] [0 0 0]]
  • 17.
    ARITHMETIC WITH NUMPY ARRAYS •Arithmetic operations with scalars propagate the scalar argument to each element in the array: arr = np.array([[1., 2., 3.], [4., 5., 6.]]) print(arr) [[1. 2. 3.] [4. 5. 6.]] print(arr **2) [[ 1. 4. 9.] [16. 25. 36.]]
  • 18.
    ARITHMETIC WITH NUMPY ARRAYS •Comparisons between arrays of the same size yield boolean arrays: arr = np.array([[1., 2., 3.], [4., 5., 6.]]) print(arr) [[1. 2. 3.] [4. 5. 6.]] arr2 = np.array([[0., 4., 1.], [7., 2., 12.]]) print(arr2) [[ 0. 4. 1.] [ 7. 2. 12.]] print(arr2 > arr) [[False True False] [ True False True]]
  • 19.
    INDEXING AND SLICING •One-dimensional arrays are simple; on the surface they act similarly to Python lists: arr = np.arange(10) print(arr) # [0 1 2 3 4 5 6 7 8 9] print(arr[5]) #5 print(arr[5:8]) #[5 6 7] arr[5:8] = 12 print(arr) #[ 0 1 2 3 4 12 12 12 8 9]
  • 20.
    INDEXING AND SLICING •An important first distinction from Python’s built-in lists is that array slices are views on the original array. • This means that the data is not copied, and any modifications to the view will be reflected in the source array. arr = np.arange(10) print(arr) # [0 1 2 3 4 5 6 7 8 9] arr_slice = arr[5:8] print(arr_slice) # [5 6 7] arr_slice[1] = 12345 print(arr) # [ 0 1 2 3 4 5 12345 7 8 9] arr_slice[:] = 64 print(arr) # [ 0 1 2 3 4 64 64 64 8 9]
  • 21.
    INDEXING • In atwo-dimensional array, the elements at each index are no longer scalars but rather one-dimensional arrays: • Thus, individual elements can be accessed recursively. But that is a bit too much work, so we can pass a comma-separated list of indices to select individual elements. • So these are equivalent: arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr2d[2]) # [7 8 9] print(arr2d[0][2]) # 3 print(arr2d[0, 2]) #3