You are on page 1of 49

Introduction to

Computational Thinking
MULLAI THIAGU

KCCS134-ICT(C)

06 APRIL, 2023
OUTLINE

 LISTS AND LOOPS

 LISTS AND BUILT-IN FUNCTIONS

 STRINGS

 RESERVED WORDS

 OPERATOR PRECEDENCE

 DEBUGGING
PART I – LISTS AND LOOPS
LOOPING OVER LISTS

 The for-in statement


makes it easy to loop
over the items in a list

for item in L:
print (item)
LOOPING OVER LISTS

 If you need only the index, use range and len:

for index in range(len(L)):


print (index)
LOOPING OVER LISTS
 If you need both the index and the item, use the enumerate function:

for index, item in enumerate(L):


print (index, item)
TAKE AWAY !

Access both
Access data Access index of data data element
element in a list element in a list and its index in
a list
PART II – LISTS AND BUILT-IN
FUNCTIONS
LIST METHODS

Method Parameters Description


append Item Adds a new item to the end of the list
insert Position, item Inserts a new item at the position given

pop None Removes and returns the last item


pop Position Removes and returns the item at position
LIST METHODS
Method Parameters Description
sort None Modifies a list to be sorted
reverse None Modifies a list to be in reverse order
index Item Returns the position of first occurrence of item

count Item Returns the number of occurrences of item

remove Item Removes the first occurrence of item


X = [ ] # empty list
X.append(14)
X.append(6)
X.append(8)
X.append(10)
print(X) [14,6,8,10]

X.insert(1,8)
print(X) [14,8,6,8,10]

X.count(8) 2
X.index(6) 2
X.reverse()
print(X) [10,8,6,8,14]
X.sort()
print(X) [6,8,8,10,14]
X.remove(6)
print(X) [8,8,10,14]
Y = X.pop()
print(Y) 14
print(X) [8,8,10]
12
 Common built in functions with Lists:

L = [1, 2, 3]

len(L) → 3 # returns the no of elements in the list

max(L) → 3 # returns the largest item in the list

min(L) → 1 # returns the smallest item in the list

sum(L) → 6 # returns the sum of all elements in the list


13
PART III – STRINGS
 Strings in python are surrounded by
either single quotation marks, or double
quotation marks
 String literal display is possible with
the print() function

 Approaches:

 Assign string to a variable

 Multiline strings

 Conditional check

 Looping through a string

 Pass a string as function argument


 Approaches:

 Assign string to a
variable
 Approaches:

 Assign string to a variable

 Multiline strings
 Approaches:

 Assign string to a variable

 Multiline strings

 Conditional check
 Approaches:

 Assign string to a variable

 Multiline strings

 Conditional check

 Looping through a string


PART IV – RESERVED WORDS
KEYWORDS IN PYTHON
 There is a restriction while naming identifiers.

 There are some restricted words that are built-in to


Python which cannot be used as an identifier.

 Python reserved words (also called keywords).

 A predefined meaning and syntax in the language


which Python uses for its syntax and internal
processing.
https://flexiple.com/python/python-reserved-words/
https://flexiple.com/python/python-reserved-words/
PART V – OPERATOR
PRECEDENCE
OPERATOR PRECEDENCE
 Combination of values, variables, operators,
and function calls is termed as an expression.

 Python interpreter can evaluate a valid expression.

 To evaluate these types of expressions there is a rule of


precedence in Python.

 Associativity: order in which Python evaluates an


expression containing multiple operators of the same
precedence.
OPERATOR PRECEDENCE
 Given an expression, the order of processing is to be
understood.
 Example:

100-40*20

 What is the expected output?


 -700 (option A)
 1200 (option B)
OPERATOR PRECEDENCE
 Given an expression, the order of processing is to be
understood.
 Example:

100-40*20

 What is the expected output?


 Answer :
 100-(40*20) = 100-800 = -700 (option A)
OPERATOR PRECEDENCE

 PEMDAS

 PARENTHESES

 EXPONENTIATION

 MULTIPLICATION

 DIVISION

 ADDITION

 SUBTRACTION
Operator Meaning

() Parentheses

** Exponent

+X,-X,~X Unary plus, Unary minus, Bitwise NOT

*,/,//,% Multiplication, Division, Floor division, Modulus

+,- Addition, Subtraction

<<,>> Bitwise shift operators

& Bitwise AND


Operator Meaning
^ Bitwise XOR

| Bitwise OR

==,!=, >, >=, <, <=, is, is not, in, Comparisons, Identity,
not in Membership operators
not Logical NOT

and Logical AND

or Logical OR
Operat
Meaning Associativity
or
() Parentheses

** Exponent Right to left


+X,- Unary plus, Unary minus,
Left-right associativity
X,~X Bitwise NOT
Multiplication, Division, Floor
*,/,//,% Left-right associativity
division, Modulus
+,- Addition, Subtraction Left-right associativity

<<,>> Bitwise shift operators Left-right associativity

& Bitwise AND Left-right associativity


Operator Meaning Associativity
^ Bitwise XOR Left-right associativity

| Bitwise OR Left-right associativity

==,!=, >, >=, <, <=, is, Comparisons, Left-right associativity


is not, in, not in Identity, Membership
operators
not Logical NOT Left-right associativity

and Logical AND Left-right associativity

or Logical OR Left-right associativity


PART VI – DEBUGGING
TESTING

 Process of running a program to try and ascertain if it


works or does not work as intended

 Aim : to show that bugs exist

 Types :
 Black-box testing
 Glass-box testing
SOFTWARE DEVELOPMENT LIFE CYCLE (SDLC)
DEBUGGING A PYTHON CODE
TYPES OF ERRORS
PROGRAMMING ERRORS ARE CALLED BUGS

THE PROCESS OF TRACKING AND CORRECTING THE BUGS IS


CALLED DEBUGGING

 Syntax errors Takeaway !

Programming is the process


 Runtime errors of gradually debugging a
program until it does what
 Semantic errors you want
 Syntax errors  Runtime errors  Semantic errors

 Syntax refers to the


rules about the  The error occurred
 These errors does not because the program you
structure of a appear until you run the
program wrote is not the program
program (also called you intended to write
exceptions)
 The compiler /
 The programmer
interpreter typically typically finds semantic
finds syntax errors  The compiler/ interpreter errors
finds runtime errors
 Ex: Missing a colon  Ex: Forgetting to divide
at the end of line  Ex: Attempting to divide by 100 when printing %
by 0 amount
ERROR MESSAGES
 ParseError:
 Happen when there is an error in the syntax of program

 TypeError:
 Happen when two objects that are not compatible are combined

 NameError:
 Occur when a variable is invoked before its values is initialized

 ValueError:
 Occur when a parameter to a function is passed and the function is
expecting a certain limitations on the values, and the value passed
is not compatible
Syntax
Error:
Name
Error :
Type
Error:
Syntax
Error:
Indent
Error:
QUERIES ?
REFERENCES

Some of the content is taken from the below online sources:

1. http://www.cs.cornell.edu/courses/cs1133/2020sp/materials/texts.php
2. https://github.com/guttag/Intro-to-Computation-and-Programming
3. https://flexiple.com/python/python-reserved-words/
4. https://littleflowercollege.edu.in/upload/e_contents/files/8fd87a00c593e8bd157ae2b0e2675f24.pdf
5. http://songyot.ece.engr.tu.ac.th/CN101/Python_Operator-ASCII-Table.pdf
6. https://www.cs2study.com/XI/Operators.pdf
7. https://www.programiz.com/python-programming/precedence-associativity

You might also like