Lectures
Lectures
Lectures
Computer Programming
References
Introduction to computing
• Computer is employed for scientific research, e-commerce, banking,
weather prediction, etc.
1. Processor
A unit that performs arithmetic and logical
operations
2. Memory
Data and program instructions are stored here.
3. Input/Output
Computer takes data from input devices, e.g.,
keyboard, mouse; and sends results to output
devices, such as, printer, terminal, etc.
Processor
This unit can perform billions of arithmetic and logical
operation per second.
The cores communicate with each other, as well as to Inside Rome 7742 processor
the RAM and PCI devices, via buses.
Estimation of the peak performance
• The clock speed of the processor is 2.5 GHz.
• Such information is useful for estimating the time for a computing job.
Memory
• The data and programs reside in computer’s memory
also called random access memory (RAM).
1 byte = 8 bits
• The CPU reads the program and data from RAM and
write the results back on it.
• OS loads as soon as a computer is turned on; this process is called boot up.
• Each of these tasks are performed using application softwares. For example,
▪ we write documents using MS Word;
▪ browse internet using Firefox, Internet Explorer, Chrome, etc.
▪ watch movies using Mplayer, VLC, etc.
System Software and Programming Languages
• At the base level, a computer understands only two alphabets—0 and 1.
• Therefore, all the instructions and data are to be converted to strings of 0’s and 1’s.
• For example, an addition of two numbers A and B involves the following operations:
1. Get numbers A and B from the memory and put them into the CPU registers.
2. Add the numbers and put result A+B into another register.
3. Transfer the result from the register to the memory.
• The addition of numbers involves bitwise operations, such as 0+0=0, 1+0=1, 1+1=0 with
a carry of 1, etc.
• It is cumbersome to write object codes for complex tasks, e.g., weather forecasting code.
System Software and Programming Languages
• Compilers and Interpreters help overcome this difficulty.
• A user writes programs in a higher-level language; then Compilers and Interpreters translate
these programs into object codes.
• These softwares save the programmer from the drudgery of writing object codes.
• The leading programming languages are C, C++, Fortran, Java, Python, Matlab, etc.
• In C, C++, and Fortran, complete computer programs is written first, after which the
programs are converted to object codes using compilers. Hence, C, C++, and Fortran are
called compiler languages.
• On the other hand, Python and Matlab interpreters execute the codes in pieces, hence
Python and Matlab are called interpreter languages.
Top programming languages
• The PYPL PopularitY of Programming
Language Index is created by
analyzing how often language
tutorials are searched on Google.
https://pypl.github.io/PYPL.html?country=IN
Python
• Guido Van Rossum created Python programming language, which
appeared in 1991 for the first time.
• It's a Good Idea to use meaningful names. If you name everything with
a single-letter, for example, you may have trouble remembering what the
name stands for later.
Line joining
• A physical line is a line that you see in a program.
• A logical line is a single statement.
• The end of a physical line marks the end of most statements (No ;).
• When a statement is too long to fit on a single line, you can join two
adjacent physical lines into a logical line by ensuring that the first physical
line has no comment and ends with a backslash (\).
• Python also joins adjacent lines into one logical line if an open
parenthesis ( ( ), bracket ( [ ), or brace ( { ) is not closed.
Characters
• Unicode (Universal Coded Character Set) is a standard that uses 16-bit
characters to represent characters on your computer. Unlike ASCII
(American Standard Code for Information Interchange), which consists of
8 bits, Unicode uses 16 bits and represents characters by integer value
denoted in base 16.
• A number does not include any punctuation and cannot begin with a
leading zero (0).
• Leading zeros are used for base 2, base 8, and base 16 numbers.
• In [125]: ord(y)
• Characters are enclosed within single quotes (‘.’) or double
• Out[125]: 38
quotes (“.”); both these quotes are used interchangeably.
• In [117]: x = '@' • In [126]: chr(38)
• Out[126]: '&'
• In [118]: ord(x)
• Out[118]: 64 • In [127]: print(chr(38))
• &
• In [119]: chr(64)
• Out[119]: ‘@’
• For a given decimal number y,
• Here, x represents character ‘@’, which is represented Python function chr(y) provides
using decimal number 64 in ASCII (American Standard the corresponding character in
Code for Information Interchange) format. ASCII representation.
• The Python function ord() provides the corresponding
ASCII value.
Python data types: Strings
• A Python string, which is an ordered sequence of
characters, is an important data types of Python.
In [16]: s1 = 'Prepare '
• A character is treated as a string with a single element. In [17]: s2 = 'very '
In [18]: s3 = 'well for MTE’
• Python strings too are enclosed within single quotes (‘
’) or double quotes (“.”). In [19]: s = s1+s2+s3
In [151]: x[1,2]
Out[151]: 9
Operation on numpy arrays
Conditional flows
• In a computer program, we perform In [56]: x = 591
operations based on certain conditions. In [57]: if x%2 == 1:
...: print('{} is odd'.format(x))
• The code segments related to conditionals 591 is odd
are called conditional or branching
program structures. In [59]: x = 58
In [60]: if x%2 == 1:
...: print('{} is odd'.format(x))
...: else:
...: print('{} is even'.format(x))
58 is even
control-flow diagram
Further conditional branches
In [67]: x = 141
In [68]: if x%2 == 0:
...: print('{} is even'.format(x))
...: elif x%3 == 0:
...: print('{} is divisible by 3'.format(x))
...: else:
...: print('{} is neither divisble by 2 nor by 3')
141 is divisible by 3
In [27]: phc_lab['BP1']
• Dictionaries are constructed using curly Out[27]: 'Tue’
brackets with each element consisting of
a key and a value, separated by a colon: In [28]: phc_lab.keys()
Out[28]: dict_keys(['EP1', 'BP1', 'EP2'])
In [29]: phc_lab.values()
• dict1 = {key1:val1, key2:val2:, key3:val3} Out[29]: dict_values([['Mon', '11-13'], 'Tue', 'Wed'])
In [30]: phc_lab.items()
Out[30]: dict_items([('EP1', ['Mon', '11-13']), ('BP1', 'Tue'), ('EP2',
'Wed')])