CISA 4309: Scripting Languages By: Dr. Smriti Bhatt
CISA 4309: Scripting Languages By: Dr. Smriti Bhatt
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
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
+ Addition a+b
− Subtraction a−b
• 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
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.