icp lecture 2
TRANSCRIPT
-
8/2/2019 ICP Lecture 2
1/37
Lets begin by discussing one
important point, which I should have
mentioned the last time Some of you are already aware of this acutely,
but for those who are not,
Let me remind you and make it clear to you thatsince you are computer science students, thiscourse is THEfundamentalcourse for you, not
basic English, business or physics etc, importantthough those courses are.
If you are going to be a computer scientist, thenyou better get serious about this course.
-
8/2/2019 ICP Lecture 2
2/37
The first lecture was just an
introduction
From this lecture we will really get cooking. You willbe given at leastone programming task at the end ofeach lecture. And we will really get into core concepts.
I will also assign reading for each lecture that youshould study. If you dont, its your loss. Just mark mywords: These initial courses are what will build yourfoundation for your ENTIRE life as a computer
scientist. I say this from experience!!!
So without any further ado, lets begin
-
8/2/2019 ICP Lecture 2
3/37
What do computer scientists do?
As I said in the last lecture, they map the
outside world to the world of computers.
Using, what?
Obviously
-
8/2/2019 ICP Lecture 2
4/37
Computational models of the world
(programs?)
So just like other scientists use abstract models andtheories to map the world to their respective domains,
we computer scientists make computational models of
the world outside.
Whenever we see a problem in the world outside, ourfirst instinct as compute scientists should be to
consider how we would program or code thisproblem. How to make the computer do what we wantit to do? This question lies at the heart of computerscience
-
8/2/2019 ICP Lecture 2
5/37
How to think like a computer scientist?
This is the goal of this course. (Suggested readingin the reference material. How to think like a
computer scientist)
Not to learn some one particular language
And please you cant depend on memorization.
This course is not for crammers. If you want to
cram, you are in the wrong field. And I say this
seriously. I am guessing there is still time, get
yourself transferred to the BBA course etc.
-
8/2/2019 ICP Lecture 2
6/37
So what is computation??
Very simply, modeling the outside world in theworld of computers. And then using that modelto analyze and solve problems.
So what kind of knowledge is modeled in acomputational model?
There are two major types of knowledge in theworld.
-
8/2/2019 ICP Lecture 2
7/37
Declarative and Imperative
Declarative: The list of prime numbers is:
2,3,5,7,11,13..
This does not tell us how we got these prime
numbers
-
8/2/2019 ICP Lecture 2
8/37
Imperative: (or algorithmic if you like)
1. Create a list of consecutive integers from 2 to n: (2, 3,
4, ..., n).
2. Initially, let p equal 2, the first prime number.
3. Starting from p, count up in increments of p and
mark each of these numbers greater than p itself in
the list. These numbers will be 2p, 3p, 4p, etc.; note
that some of them may have already been marked.
4. Find the first number greater than p in the list that is
not marked; let p now equal this number (which is
the next prime).5. If there were no more unmarked numbers in the list,
stop. Otherwise, repeat from step 3.
This is basically a recipe, an algorithmwe discussedthis last time.
-
8/2/2019 ICP Lecture 2
9/37
Or a simple recipe
Add sugar and milk
Beat egg yolk till the its smooth (or we can
say, while egg yolk is not smooth, keep
beating.
If sugar is not to taste, add more
And so on
-
8/2/2019 ICP Lecture 2
10/37
How to capture this in a mechanical
process
One could build a circuit board?
Components to store data.
Wires to move data around. Something for multiplication
Something to check whether value is marked
or not.
-
8/2/2019 ICP Lecture 2
11/37
This was an example of early
computers
Fixed Program Computers
Such as a calculator? Sure!
It is built for a specific purpose. You cannot do anythingelse on it.
Play Need For Speed on it? Powerpoint presentation?
No, it is meant for one purpose. So it is notprogrammable.
-
8/2/2019 ICP Lecture 2
12/37
What if
We have a machine that can take an
algorithm, a sequence of steps and execute it.
So it can take any input and work accordingly.
Of course, there is the interpreter in the
modern computer that does this.
How does it work? A brief overview
-
8/2/2019 ICP Lecture 2
13/37
Memory
Control Unit ALU
INSTRUCTION 1
INSTRUCTION 2
INSTRUCTION 3
And so on
Program
Counter
INPUT OUTPUT
-
8/2/2019 ICP Lecture 2
14/37
So as discussed earlier
We need a programming language to code
these instructions
What is a programming language?
-
8/2/2019 ICP Lecture 2
15/37
Programming language vs
programming skills and techniques
The main thing is to have a programmingmindset and to know the common
programming techniques across alllanguages.
So our goal is not to learn any onelanguage but rather equip ourselves insuch a manner that we can program in anylanguage that we are required to.
-
8/2/2019 ICP Lecture 2
16/37
Therefore
Learning how to program, concepts, skill...
These are uniform across all languages.
What can be done in one language can be
done in another.
-
8/2/2019 ICP Lecture 2
17/37
1. High level vs. Low level languages
What does high level mean? (I told you the
last time)
2. General purpose vs. targeted languages
3. Interpreted vs. Compiled Language
-
8/2/2019 ICP Lecture 2
18/37
Syntax vs Semantics
Syntax: Rules governing the sequence of words
in a sentence.
Semantics: What sentence are meaningful?
Something that is correct syntactically might notbe meaningful semantically. E.g.
Colorless green ideas sleep furiously.
-
8/2/2019 ICP Lecture 2
19/37
Tasks that we will undertake to delve
into the world of programming
1) Set up a programming environment.
2) I said last time, we shall be using C. Two
reasons. Major reason: It was written in thecourse outline that I was given
3) Minor reason: I was also taught C in myundergraduate. I think most teachers usuallyknow only C or at the most, C and C++
4) However, a bit of research has shown
-
8/2/2019 ICP Lecture 2
20/37
So we shall be doing Python
Why?
Its simple,
Its fun
It lets you do a lot of powerful stuff simply
You learn all the tools/skills that you need tobecome a good programmer
-
8/2/2019 ICP Lecture 2
21/37
Print hello world in three different
languages
C language requires this program to be wrapped in
a function and preceded by a preprocessor
directive:
#include
main()
{
printf("Hello World!);
}
-
8/2/2019 ICP Lecture 2
22/37
In Java, the situation is terrible because our
code must be inside of a class:
public class helloWorld
{
public static void main(String [] args)
{
System.out.println("Hello World!");
}
}
-
8/2/2019 ICP Lecture 2
23/37
And in Python?
print Hello World!
Simple isnt it?
-
8/2/2019 ICP Lecture 2
24/37
Basic things in Python
Data types
Two major kinds of primitive data types
Numbers (example 2, 3, 6535, 0.001)
and
Strings (Rameez rahman, introduction tocomputer science and programming.
-
8/2/2019 ICP Lecture 2
25/37
As discussed last time. Just like for C
A number can have many data types.
int (2, 3, 1000)
float (27.65, 5.6)
and others.
-
8/2/2019 ICP Lecture 2
26/37
We can create custom data types as I
mentioned in the last lecture.
However, no matter how complex data types
we end up building, they will in the end be
made up of three basic data types Numbers (float, int etc)
Strings
Booleans (which we will discuss when we gothrough the code file)
-
8/2/2019 ICP Lecture 2
27/37
Operators Associated with different data types are different
operators.
Operators do certain jobs.
E.g. + . So the operator + between 2 and 3, willgive the sum of 2 and 3
2+3 (this will give you 5)
Expressions are made up of operand andoperators
2+3 is a programming expression. So iscomputer + programming
-
8/2/2019 ICP Lecture 2
28/37
Some examples
Example 2.5 + 2.6
Type conversion: str(10) + is a small number
works
Why 10 + number does not work.
Common sense: Python does type checking.
-
8/2/2019 ICP Lecture 2
29/37
Weak vs strong typing
Different languages have different kinds oftype checking
No matter which language you are using, bestrict about types. So try not to do thefollowing:
student < 3
Even though this works in python, but it isnonsensical more or less and can give errors inother languages.
-
8/2/2019 ICP Lecture 2
30/37
15/4
15%4
122 + 4 * 5
Operator precedence. Remember BODMAS
-
8/2/2019 ICP Lecture 2
31/37
Variablesvalue assignment
We use variables to store values so that wecan use them later.
= < variable value>
firstVariableString = Cool
number = 1.01
-
8/2/2019 ICP Lecture 2
32/37
How does assignment work deep
down
When we write for example x = 10
What we do is that in the memory somewhere isplaced the object 10, and we bind the variable xwith this object. So x points to 10 or is linked to10. we will come to this later.
y = x. so now y becomes 10 And later if we do x = 20
What does y become?
-
8/2/2019 ICP Lecture 2
33/37
Whats the type of variables?
So for example what was the type of x?
In C, if you remember we would have written
something like,
int x; So we knew that the type of x is an integer.
But in python x simply inherits its type from the
value.
So if x = 10, then x is a number and if we write
x = computer then x is a string.
Some advice:
-
8/2/2019 ICP Lecture 2
34/37
Some advice:
Dont change types of the same variable
arbitrarily in a program. It makes it harder to
debug
Try to give intuitive variable names
If a varialbe will store the humidity levels,
then
call it humidityLevel (or humid) rather than x.
Also, you can choose any variable name that
you want apart from keywords that python
uses.
-
8/2/2019 ICP Lecture 2
35/37
Lets come to statements to get some
real programming
An expression is a legal command that python
can interpret.
We saw some earlier.
Print
Assignment
There are of course many other
-
8/2/2019 ICP Lecture 2
36/37
What we have done until now is what is called
a straight line program.
We are simply executing one instruction after
another
This is not very interesting.
Lets go back to algorithms and recipes.
-
8/2/2019 ICP Lecture 2
37/37
Branching programs
Something that can change the order of
instructions based on some test.
And that test is usually a value of a variable
Lets now go to our code file.