0% found this document useful (0 votes)
39 views

CISA 4309: Scripting Languages By: Dr. Smriti Bhatt

The document provides an overview of scripting languages and key concepts in Python including variables, data types, expressions, functions, and modules. It discusses numeric, string, list, tuple, and dictionary variables. Integer and floating-point number data types are introduced along with type conversions. The document also covers arithmetic, comparison, assignment, and logical operators. Finally, it discusses functions, the math module, and program structure in Python using if/else statements and docstrings.

Uploaded by

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

CISA 4309: Scripting Languages By: Dr. Smriti Bhatt

The document provides an overview of scripting languages and key concepts in Python including variables, data types, expressions, functions, and modules. It discusses numeric, string, list, tuple, and dictionary variables. Integer and floating-point number data types are introduced along with type conversions. The document also covers arithmetic, comparison, assignment, and logical operators. Finally, it discusses functions, the math module, and program structure in Python using if/else statements and docstrings.

Uploaded by

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

CISA 4309: Scripting Languages

By
Dr. Smriti Bhatt
Objectives
• Variables, Strings, Assignments
• Expressions, Arithmetic Expressions
• Type Conversions
• Functions and Modules
Variables Examples
• Numeric Variables
• X1=1 # int
• Y1=2.8 # float
• Z1=1j # complex
• X2=1 # int
• Y2=35656222554887711 # int
• Z2=-3255522 # int
• String Variables
• E.g., name = “Ken”
• List: is collection of values of any types,
• E.g., Numbers = [‘One’, ‘Two’, ‘Three’],
• Tuple: similar to list, but list are mutable and tuples immutable, indexed by integers, defined using ()
• E.g. (2, ‘Welcome’, 8, ‘TAMUSA’)
• Directory: each element is key: value pair, created using {}
• E.g., { 1: ‘one’, 2:’two’, 3: ’three’}
Numeric data types
• Number objects are created when you assign a value to them. For example:
• Var1 = 1, Var2 = 10
• You can also delete the reference to a number object by using the del
statement. The syntax of the del statement is:
• del Var1[,Var2[,Var3[...., VarN]]]]
• Example: del Var1, Var2 Q. Run : py_section04_example_02.py

• Integers: in real life, the range of integers is infinite


• A computer’s memory places a limit on the magnitude of the largest
positive and negative integers
• Python’s int typical range: 2131 to 231  1
• Integer literals are written without commas
Floating-Point Numbers
• Real numbers have infinite precision
• The digits in the fractional part can continue forever
• Python uses floating-point numbers to represent real numbers
• Python’s float typical range: 10 to 10 and
308 308

• A floating-point number can be written using either ordinary decimal


notation or scientific notation
Decimal Notation Scientific Notation Meaning
3.78 times 10 to the 0

3.78 3.78e0
3.78 times 10 to the 1

37.8 3.78e1
3.78 times 10 Cubed

3780.0 3.78e3
3.78 times 10 to the minus 1

0.378 3.78e−1
3.78 times 10 to the minus 3

0.00378 3.78e−3
Character Sets
• In Python, character literals look just like string literals and are of the string
type.
• They belong to several different character sets, such as
• ASCII set and Unicode set
• ASCII character set maps to a set of integers
• ord and chr functions convert characters to and from ASCII
Example:
>>> ord(‘A’)
65
>>> chr(65)
‘A’
>>> chr(66)
‘B’
Expressions
• Types of expressions:
• A literal evaluates to itself
• A variable reference evaluates to the variable’s current value
• Expressions provide easy way to perform operations on data values to
produce other values
• When entered at Python shell prompt:
• an expression’s operands are evaluated
• its operator is then applied to these values to compute the value of the
expression
• E.g., 2 + 2
Arithmetic Expressions – I
• An arithmetic expression consists of operands and operators
Operator Meaning Syntax

− Negation −a
Run: py_section05_example_01.py
** Exponentiation a ** b

* Multiplication a*b

/ Division a/b

// Quotient a // b

% Remainder or modulus a%b

+ Addition a+b

− Subtraction a−b

Extensive list of Arithmetic operators in Python Reading document – Page 18


Arithmetic Expressions – II
• Precedence rules:
• ** has the highest precedence and is evaluated first
• Unary negation is evaluated next
• *, /, and % are evaluated before + and −
• + and − are evaluated before =
• With two exceptions, operations of equal precedence are left associative, so
they are evaluated from left to right
• ** and = are right associative
• You can use () to enforce the order of evaluation
Arithmetic Expressions – III
• When both operands of an expression are of the same numeric type,
the resulting value is also of that type
• When each operand is of a different type, the resulting value is of the
more general type
• Example: 3 / 4 is 0, whereas 3 / 4.0 is .75
• For multi-line expressions, use a \
• Example:
>>> 3 + 4 * \
2 ** 5
Output: 131
Mixed-Mode Arithmetic
• It involves integers and floating-point numbers:
Example:
>>> 3.14 * 3 ** 2
Output: 28.26
• You must use a type conversion function when working with input of
numbers
• It is a function with the same name as the data type to which it converts,
int or float function
Type Conversions - I

Extensive list provided in Python Reading Document: Page 17

• Need to be careful with int and round function (int converts a float to
an int by truncation, not by rounding)
• Example:
>>> int(6.75)
6
>>> round(6.75)
7
Type Conversions - II
• Type conversion also occurs in the construction of strings from
numbers and other strings
>>> profit = 1000.55
>>> print(‘$’ + profit)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: cannot concatenate ‘str’ and ‘float’ objects

Q1. How do you fix it?


• Hint: use str function
Comparison Operators
• Allows to compare the values of the operands
• Run: py_section05_example_02.py
From Python Reading Doc
Assignment Operators

Run: py_section05_example_03.py
From Python Reading Doc
Logical Operators

Run: py_section05_example_04.py
From Python Reading Doc
Functions and Modules
• Python includes many useful functions, which are organized in libraries of
code called modules
• A function is chunk of code that can be called by name to perform a task
• Functions often require arguments or parameters
• Arguments may be optional or required
• When function completes its task, it may return a value back to the part of
the program that called it
• Using help function to learn about other function’s arguments:
• >>> help(round)
Q2. What output do you get? Try help for other functions or even reserved words that
you know – print, input, for, while
The Math Module - I
• Functions and other resources are coded in components called modules
• Functions like abs and round from the _builtin_ module are always
available to use
• The math module includes functions that perform basic mathematical
operations
• To use a resource from a module, you write the name of a module as a
qualifier, followed by a dot (.) and the name of the resource
• Example: math.pi
>>>math.pi
3.1415926535897931
>>>Math.sqrt(2)
1.4142135623730951
The Math Module - II
• You can avoid the use of the qualifier with each reference by
importing the individual resources
• Example:
>>> from math import pi, sqrt
>>>print(pi, sqrt(2))
Output: 3.14159265359 1.41421356237
• You may import all of a module’s resources to use without the
qualifier
• Example: from math import *
Suites in Python
Multiple Statement Groups as Suites
• A group of individual statements, which make a single block of code are called suites in Python.
• Compound or complex statements, such as if, while, def, and class, are those which require a
Header Line and a suite.
• Header lines begin with the keyword/reserved word and terminate with a colon ( : ) and are
followed by one or more lines which make up the suite.
• For example:
- if expression :
suite
- elifexpression :
suite
- else :
suite
if, if-else and if-elif-else statements
• The if statement: contains a logical expression using which data is
compared and a decision is made based on the result of the
comparison.
• Syntax:
• if expression:
statement(s)
• If the Boolean expression evaluates to true, then the block of
statement(s) inside the if statement will be executed.
• If Boolean expression evaluates to false, then the first set of code
after the end of the if statement(s) will be executed.
• Run: py_section_06_example_01.py
Program Format and Structure
• Start with comment with author’s name, purpose of program, and
other relevant information using docstring
• A docstring is string enclosed by triple quotation marks and provides program
documentation
• Then, include statements that:
• Import any modules needed by program
• Initialize important variables, suitably commented
• Prompt the user for input data and save the input data in variables
• Process the inputs to produce the results
• Display the results
Running a Script from a Terminal Command
Prompt
• A way to run a Python script:
• Open a terminal command prompt window
• Click in the “Type here to search” box, type Command Prompt, and click Command
Prompt in the list
• Navigate or change directories until the prompt shows directory that contains the
Python script
• Run the script by entering:
• python scriptname.py
• Python installations enable you to launch Python scripts by double-clicking
the files from the OS’s file browser
• May require .py file type to be set
• Fly-by-window problem: Window will close automatically
• Make sure you right click and open the script in the editor to edit
Summary - I
• Literals are data values that can appear in program
• The string data type is used to represent text for input and output
• Escape characters begin with backslash and represent special
characters such as delete key
• A docstring is string enclosed by triple quotation marks and provides
program documentation
• A function call consists of a function’s name and its arguments or
parameters
• May return a result value to the caller
Summary - II
• Comments are pieces of code not evaluated by the interpreter but
can be read by programmers to obtain information about a program
• Variables are names that refer to values
• Some data types: int and float
• Arithmetic operators are used to form arithmetic expressions
• Operators are ranked in precedence
• Mixed-mode operations involve operands of different numeric data
types
• Type conversion functions can be used to convert a value of one type
to a value of another type after input
Summary - III
• A module is a set of resources
• Can be imported
• A semantic error occurs when the computer cannot perform the
requested operation
• A logic error produces incorrect results
References
• Fundamentals of Python: First Programs, 2nd Edition, Kenneth A. Lambert, Cengage.
• Python Reading and Practice notes in Blackboard.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy