cs1032 2013batch lecture 1
DESCRIPTION
pythonTRANSCRIPT
CS1032
Programming Fundamentals
Prof. Sanath Jayasena Dept. of Computer Science & Engineering
Semester 1 (2013 Batch)
[Mar - July, 2014]
1. Course Description
2. Lecture Session 1
1. Course Description
CS1032
Programming Fundamentals
(3 credits)
Can be found in:
• the Course Outline document (copies given)
• the course LMS site at http://online2.mrt.ac.lk/course/view.php?id=2
Learning Outcomes
• After completing this course, the students
will be able to:
– Device algorithms to solve simple engineering
and computational problems
– Develop programs from algorithms using a
programming language such as Python
– Use an Integrated Development Environment
(IDE) for program development
3 Sanath Jayasena, March 2014
Main Staff
• Coordinator: Prof. Sanath Jayasena
• Staff for lecture sessions
– Prof. Sanath Jayasena, Dr. Chathura De
Silva, Dr. Shehan Perera, Dr. Malaka Walpola
• Labs coordinated by:
– Mr. Adeesha Wijayasiri
• Other staff (e.g., Instructor in-charge)
– Get to know in the lab
4 Sanath Jayasena, March 2014
Course Material
• Course on the LMS
– http://online2.mrt.ac.lk/course/view.php?id=2
– Contain all resources and activities
• Two documents will be distributed
– Course Notes for CS1032 (first, Part A)
– Lab Exercises for CS1032
– Have them with you in lectures, lab sessions
– Remainder will be given later
5 Sanath Jayasena, March 2014
Additional Reading
• Learning Python, 5th Edition by Mark Lutz, O'Reilly Media, 2013
• Introduction to Computing Using Python:
An Application Development Focus, by
Ljubomir Perkovic, Wiley, 2012
• Python Tutorials and Documentation
– http://www.tutorialspoint.com/
– http://docs.python.org/2/tutorial/
– http://docs.python.org/2/contents.html
6 Sanath Jayasena, March 2014
Method of Assessment
• Credits:3
• Final written examination: 80%
– 3 hours; mix of MCQ, short and structured Qs
• Continuous Assessments (CA): 20%
– 5 Evaluated Labs, 1% each 5%
– Programming Assignment 1 7.5%
– Programming Assignment 2 7.5%
7 Sanath Jayasena, March 2014
Assessment of Programs
1. Problem not attempted to satisfactory level 0% - 25%
2. Problem attempted, code has syntax errors30% max
3. Problem attempted, no syntax errors 50% max
4. (3) Above + code runs without crashing or unexpected
termination + some results/output may not be correct
70% max
5. (3) Above + code runs without crashing or unexpected
termination + results/output are correct 90% max
6. Programming style (comments, variable naming…)
10% max
8 Sanath Jayasena, March 2014
Rules, Regulations
• 35% each from CA and Exam required to
pass the course module
• Must comply with rules of UoM
– Serious consequences if violated
• E.g., copying, letting copy, plagiarism, conduct
during exams and CA work, sharing of computer
passwords, unauthorized access
• Missed lab (CA) session?
– Another chance may be given based on
• Reason, evidence, how soon informed
9 Sanath Jayasena, March 2014
Course Schedule
• Lecture sessions:
– 2 hours per week
• Group A: Wed 08.15am-10.15am, Aud-1
• Group B: Fri 10.15am – 12.15pm, Aud-2
• Group C: Mon 08.15am-10.15am, Aud-2
• Venues: Auditoriums 1, 2
10 Sanath Jayasena, March 2014
Course Schedule
• Labs: 3 hours per week (CSE Level 1 Lab, CITeS Lab)
11 Sanath Jayasena, March 2014
Group A-1 Tuesday 08.15-11.15am
Group A-2 Friday 12.15-03.15pm
Group A-3 Thursday 01.15-04.15pm
Group A-4 (ER) Wednesday 03.15-06.15pm
Group B-1 Thursday 08.15-11.15am
Group B-2 Wednesday 12.15-03.15pm
Group B-3 Monday 03.15-06.15pm
Group B-4 (TL) Monday 12.15-03.15pm
Group B-5 Monday 08.15-11.15am
Group C-1 Friday 03.15-06.15pm
Group C-2 Wednesday 08.15-11.15am
Group C-3 Friday 08.15-11.15am
Group C-4 (TX) Tuesday 01.15-04.15pm
Course Outline (Lectures)
– Overview of programming
• Algorithms and problem solving
• Introduction to C
– Programming in Python
• Data types, expressions, data representation
• Control structures: selection, loops, functions
• Lists, input, output and file handling
– Computer system hardware
– Beyond basics: Introduction to
• Problem solving, algorithms, data structures,
software engineering
12 Sanath Jayasena, March 2014
Course Outline (Labs)
• Lab 1, 2: Introduction to Python Programming
• Lab 3: Conditional (Selection) Structures
• Lab 4: IDEs and Debuggers
• Lab 5: Loop Control Structures (E)
• Lab 6: Lists (E)
• Prog. Assign-1 (7.5%)
• Lab 7: Functions (E)
• Lab 8: File Handling (E)
• Lab 9: File Handling & Functions (E)
• Lab 10:
• Prog. Assign-2 (7.5%)
See the Course
Schedule or the
LMS for detailed
schedule
13 Sanath Jayasena, March 2014
On Labs, CA Work…
• Be punctual
• Follow the “Lab Rules”
• Ask for clarifications if you need any
• Deadlines must be met
• Don’t cheat
• Follow the instructions given for lab
sessions (you have to READ!)
• Give us feedback Any questions?
14 Sanath Jayasena, March 2014
2. Lecture Session 1
Background,
Overview of “C”,
Introduction to Python
Outline
• Background
– Programs
– Program development
• Program translation
– Compilation vs Interpretation
• Overview of the “C” language
• Introduction to Python
16 Sanath Jayasena, March 2014
Background
• Program
– an ordered sequence of instructions that the
hardware can execute
– tells a computer (how) to perform a task
• A computer program is expressed using a
programming language
• Computer programming
– The art of developing computer programs
– Core part of software development
17 Sanath Jayasena, March 2014
Introduction
• Programmers develop programs,…by
– developing an algorithm to solve the problem
(to complete the given computational task)
– converting the algorithm into a program
– testing the program to see whether it works as
expected and taking corrective actions if not
• Algorithm
– a sequence of a finite number of well-defined
steps for solving a problem
18 Sanath Jayasena, March 2014
Program Development
1. Define the problem
2. Outline the solution
3. Develop an algorithm
4. Test the algorithm for correctness
5. Code the algorithm in a suitable language
6. Ensure program has no syntax errors
7. Compile to generate translated code (optional)
8. Run the program
9. Test and debug the program
10.Document and maintain the program
As needed
19 Sanath Jayasena, March 2014
Developing an Algorithm
• Algorithm should consist of a precise, clear and
finite sequence of steps to solve the problem
• More than one algorithm could exist for a given
problem
• Developing an algorithm is the most important
step in the process of developing a program
• For a complex problem, developing an algorithm
is generally the most difficult part
20 Sanath Jayasena, March 2014
Expressing an Algorithm
• Pseudocode (a structured form of the
English language) can be used
• Example
Step 1. Start
2. Input r
3. Compute circumference: c 2 * PI * r
4. Compute area: a PI * r * r
5. Output c and a
6. Stop
• ‘PI’ is the value of π; ‘*’ is the multiplication operator 21 Sanath Jayasena, March 2014
Expressing an Algorithm
• Flowcharts can also be used
Symbol Description
Flow is used to connect different symbols and
indicates the direction of flow of the algorithm.
Process symbol can be used for a single step or an
entire sub process.
Decision indicates a choice and selection of a path
by the algorithm based on a certain criteria.
Input / Output represents input or output data to the
algorithm.
Start / Stop represents the start / end of the
algorithm.
22 Sanath Jayasena, March 2014
Example 1
Start
Stop
Output c and a
c 2 * PI* r
a PI * r * r
Input radius r
23 Sanath Jayasena, March 2014
Example 2
(a) Pseudo code
1. Start
2. Num 1
Total 0
3. Total Total + Num
Num Num + 1
4. If Num ≤ 100 go to Step 3
{ implied: else go to Step 5 }
5. Output Total
6. Stop
Start
Stop
Output Total
Num 1
Total 0
Num ≤
100?
Total Total + Num
Num Num + 1
Yes
No
(b) Flowchart Compute the sum of numbers 1-100
24 Sanath Jayasena, March 2014
Program Translation
• Programs developed in human-readable
high-level languages (source code)
• Computer understands 1’s and 0’s
– the machine language or machine code
• Program translation options:
– Compilation by a compiler
– Interpretation by an interpreter
– […depending on whether we have a
compiled-implementation or an interpreted-
implementation for the source language] 25 Sanath Jayasena, March 2014
Compilers & Compilation
26 Sanath Jayasena, March 2014
Compilation (Translation)
Source
Program Executable
Machine Code Compiler
Execution
Input Output Executable
Machine Code
Compilation
• Compilation: source code program machine-
code (object code) program
• Linking: object code with other object code
executable machine-code program
• Machine code can be saved on disk (as object
files or executable file) and reused
• No recompilation unless the source changed
• Example compiled-implementations:
– FORTRAN, COBOL, C, C++, Java and Pascal
27 Sanath Jayasena, March 2014
Translation and Execution
Interpreters & Interpretation
Source
Program
Interpreter
Input
Output
28 Sanath Jayasena, March 2014
Interpretation
• For each source code statement
– First, translate into machine-code
– Next, execute the machine code
• Translation done for every execution
– Machine-code of the full original source
program is neither produced nor saved
– Execution slower than compiled code
• Example interpreted-implementations
– BASIC, Perl, Python, Ruby,…
Sanath Jayasena, March 2014 29
• Syntax errors: violation of syntax
(grammar) rules of the source language
– Compiler/interpreter will complain; fix by
editing the source program
• A syntactically correct program may still
have bugs (other errors or faults)
– While the program is running runtime errors
may cause unexpected termination
– Program not doing what it is supposed to do
due to logic errors
Errors in Programs
30 Sanath Jayasena, March 2014
Overview of “C”
• “C” : one of the most widely used languages
– Stable, portable, standardized
• Various languages have branched away
from C; e.g., C++, Java, C#
• C programmers have power and flexibility to
make programs fast and efficient
– Comes at a cost: strongly-typed, strict syntax,
burden of managing memory,…
31 Sanath Jayasena, March 2014
Creating a C program
• A programmer uses a text editor program
to create/modify C source (code) files
– C source files are given the extension “.c”
• Then the programmer must invoke the C
compiler to compile it
– Depending on the “development
environment”, compilation methods may differ
– E.g., command-line or graphical interfaces
32 Sanath Jayasena, March 2014
• To compile using the gcc compiler at the command-line (shell) prompt, type $ gcc SourceFileName.c
• If there are no errors, this will produce an executable file named a.out
• To execute the program type at the command-line prompt
$ ./a.out
Compiling and Running
a C Program on Linux
33 Sanath Jayasena, March 2014
Three Stages of Translation
• The translation of a C program in reality
consists of 3 stages (one of them is
compiling)
1. Preprocessing
2. Compiling
3. Linking
34 Sanath Jayasena, March 2014
1-35 Sanath Jayasena, March 2014
1. Preprocessing
• Performed by a program called the preprocessor
• Modifies the source code (in memory) according
to preprocessor directives embedded in source
– E.g., #define, #include, #ifdef
– Strips comments and white space from the code
• The source code stored on disk is not modified
• May issue error messages and stop without
success if problems encountered
36 Sanath Jayasena, March 2014
2. Compiling
• Performed by the compiler
• Translates the preprocessor-modified source code into object code (machine code), which is not “executable” as yet
• Checks for syntax errors and violation of other language rules
– May issue warnings and still proceed
– May terminate without producing object code and issue error messages
37 Sanath Jayasena, March 2014
• Links the program object code with other object code to produce an executable file
– i.e., link with the standard library, other libraries, object files you created (“.o” files)
• Saves the executable code on disk
– On Linux, this file is default named a.out
• If any link errors are encountered
– No executable file will be generated; error messages will be displayed
3. Linking
38 Sanath Jayasena, March 2014
An Example C Program
/* Filename: hello.c
Author: Brian Kernighan & Dennis Ritchie
Description: Prints the greeting “Hello, World!”
*/
#include <stdio.h>
int main ( void )
{
printf ( “Hello, World!\n” ) ;
return 0 ;
}
Comment Preprocessor
directive
Keywords:
int void return Functions:
main( ) and printf( )
2 statements;
each ends with “;”
39 Sanath Jayasena, March 2014
Our Example C Program
• The program displays the message “Hello,
World!” on screen using printf( ) function
• “int main (void)” means main( ) function
– returns an integer value
– accepts no arguments, i.e., its arg list is void
• “return 0” is to return value 0
– to the environment in which it is invoked
– indicates normal/successful end
40 Sanath Jayasena, March 2014
Our Example C Program
• Within printf( ), “\n” is used to break the
displayed text line and to go to a new line
– Without the “\n”, an edit-compile-execute
session on Linux would look as follows
$ vim HelloWorld.c $ gcc HelloWorld.c $ ./a.out Hello World!$
Edit using “vim”
Compile using “gcc”
Execute a.out
Notice where the
prompt $ is now
41 Sanath Jayasena, March 2014
Anatomy of a C Program
• C Language keywords
• main( ) function required in every program
• Pre-processor directives
– E.g., #include <stdio.h> so that we can use
printf( ) function
– stdio.h : standard input output header file
42 Sanath Jayasena, March 2014
Anatomy of a C Program
• Comments
– Multi-line block comments within “/*” and “*/”
– Single-line comments starting with “//”
• Tokens: elements identified by the
compiler in the source code
– Keywords (also known as reserved words),
literals, identifiers and operators
43 Sanath Jayasena, March 2014
C Keywords
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
Note that C is case-sensitive; so “int” is a valid keyword
whereas “Int”, “INT” and “InT” are not
44 Sanath Jayasena, March 2014
Literals
(Factual Data/Constants)
• Numeric literal : an uninterrupted
sequence of digits (may contain a period)
– E.g., 123, 10000 and 99.99
• Character literal : a single character
surrounded by single quotation marks ‘ ’
– E.g., ‘a’, ‘G’, ‘$’ , ‘4’ , ‘?’, ‘{’, ‘@’, ‘+’
• String literal : a sequence of characters
surrounded by double quotation marks “ ”
– E.g., “I like ice cream.”
45 Sanath Jayasena, March 2014
Identifiers
• Identifiers are also referred to as names
– Variable names, function names
– Examples: last_name, total, x1, printf, main
• A valid identifier : a letter followed by a
sequence of letters, digits or underscore
(_) symbols
• C is case sensitive identifier abc is
different from ABC or Abc
46 Sanath Jayasena, March 2014
Operators
• Used with operands to build expressions
– E.g., “4+5” operands 4 and 5, operator +
• C supports a large number of
mathematical and logical operators such
as +, -, *, /, %, ^, &, && , |, ||, ?
• Symbols like {, }, (, ), [, ], ;, ;,... may also
be considered as operators
• Will be discussed later in detail
47 Sanath Jayasena, March 2014
C Tokens: Summary
48 Sanath Jayasena, March 2014
Python: Quick Overview
• Python : general-purpose, high-level
• Design philosophy includes:
– Code readability
– Expressing concepts in small amount of code
• Supports many programming paradigms
– Object-oriented, functional, imperative
• Has dynamic typing, automatic memory
management
Sanath Jayasena, March 2014 49
How to Use Python
1. Interactive mode
– Work with the interpreter interactively
– Enter expressions, statements, commands
one at a time, and wait for the interpreter
2. Command-line mode
– “programming mode”
– Edit a Python program into a file
– Invoke the interpreter with the program file as
input
Sanath Jayasena, March 2014 50
Comparing C, Python?
C Python
Time to learn High Low
Time to code a program High Low
Code size (# of lines) High Low
Efficiency
(e.g., speed) High Low
Sanath Jayasena, March 2014 51
Conclusion
• We discussed
– Course details of CS1032
– Background
– Overview of C
– Quick introduction to Python
• Next time
– Continue with Python programming
• Operators, expressions
52 Sanath Jayasena, March 2014