Download Programming and Data Representation (Part-2)

yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Programming and Data
Representation (Part-2)
Functions are "self contained" modules of code that
accomplish a specific task. Functions usually "take in"
data, process it, and "return" a result.
Once a function is written, it can be used over and over
and over again. Functions can be "called" from the inside
of other functions.
For example computing the square root of a number.
In general, we don't care how a function does what it
does, only that it "does it"!
When a function is "called" the program "leaves" the current section of code and
begins to execute the first line inside the function. Thus the function "flow of control"
○ The program comes to a line of code containing a "function call".
○ The program enters the function (starts at the first line in the function code).
○ All instructions inside of the function are executed from top to bottom.
○ The program leaves the function and goes back to where it started from.
○ Any data computed and RETURNED by the function is used in place of the
function in the original line of code.
In pseudocode, a function definition is written as:
FUNCTION <functionIdentifier> RETURNS <dataType> // function
<statement(s)> // function body
RETURN <value>
Functions in Python
def getMax( set ):
max = set[0]
i = 1
while( i < 5 ):
if( max < set[i] ):
max = set[i]
i = i + 1
return max
set1 = [10, 20, 30, 40, 50]
set2 = [101, 201, 301, 401, 501]
max = getMax(set1)
print "Max in first set = ", max # Process first set of
numbers available in set1[]
max = getMax(set2)
print "Max in second set = ", max # Now process second
set of numbers available in set2[]
Built-in functions
Programming environments provide many built-in
functions. Some of them are always available to use;
some need to be imported from specialist module
Built-in functions
Slicing in Python
In Python a subsequence of any sequence type (e.g.
lists and strings) can be created using ‘slicing’.
For example, to get a substring of length L from
position P in string S we write S[P : P + L].
Slicing in Python
ThisString[2:] CDEFG
ThisString[:2] AB
ThisString[-2:] FG
ThisString[:-2] ABCDE
Leap Year Calculation
Leap Year Calculation Flowchart
Code for Leap Year
year = int(input("enter the year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print('%0.0f is a leap year' % year)
print('%0.0f is not a leap year' % year)
print('%0.0f is a leap year' % year)
print('%0.0f is not a leap year' % year)
Converting a string to a number
Sometimes a whole number may be held as a string.
To use such a number in a calculation, we first need to
convert it to an integer. For example, these functions
return the integer value 5 from the string "5"
Consider the code:
grades = ["82", "84", "71", "64", "66"]
new_grades = [int(g) for g in grades]
[82, 84, 71, 64, 66]
This code has successfully converted list of strings into a list of integers.
Random number generator
Random numbers are often required for simulations. Most
programming languages have various random number generators
available. As the random numbers are generated through a program,
they are referred to as ‘pseudo-random’ numbers.
Random number generator
A procedure allows us to group a block of code under a name,
known as a procedure name. We can call the block of code from
anywhere in the program to execute the instructions it contains. We
can also pass values to the procedure to change how it works.
def showMenu():
print('Main Menu')
print('1. Play game')
print('2. View high scores')
print('3. Quit')
pi = 3.14
def showAreaOfCircle(radius):
area = pi * radius * radius
def updatePi(newPi):
global pi
pi = newPi
print('Area: ' + str(area))
Creating 1D arrays
List1 = []
Creating 1D arrays
String[] list1 = {"","",""};
int[] list2;
list2 = new int[5];
int[] list3;
list3 = new int[100];
String[] aList;
aList = new String[25];
Accessing 1D arrays
NList[24] = 0
AList[3] = "D"
In Python, you can print the whole contents of a list using
print(List). In VB.NET and Java, you need to use a loop to
print one element of an array at a time.