WEEK9 (1) Lab Questions and Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

What is

Computer
Science?

CS121 - Introduction to Computer Applications and Programming


Definition
Computer science is a discipline that involves the understanding and
design of computers and computational processes.

CS121 - Introduction to Computer Applications and Programming 2


Our goals
Our goals are not to just write copious amounts of code, our goals are
to:

➢ increase our problem solving skills


➢ design good solutions to problems
➢ test somehow how well they are indeed solutions to the problem

CS121 - Introduction to Computer Applications and Programming 3


An analogy
Let us say that you have signed up to study French poetry (how about
Marot) in the original.

You have two problems:


◦ you don’t speak French
◦ you don’t know much about poetry

CS121 - Introduction to Computer Applications and Programming 4


How does this apply
You have two related problems:
◦ the syntax of French is something you have to learn
◦ the semantics of poetry is something you have to learn

You have two problems you have to solve at the same time.

CS121 - Introduction to Computer Applications and Programming 5


A similar approach in programming
➢ You have to learn the syntax of a particular programming language
◦ many details about the language, how to debug and use it

➢ You have to learn about problem solving and how to put it down on
computer.

CS121 - Introduction to Computer Applications and Programming 6


Introduction to Algorithms
If you want a computer to perform a task, you start by writing an
algorithm
An Algorithm is:
◦ a sequence (the order mattering) of actions to take to accomplish the given
task
◦ An algorithm is like a recipe; it is a set of instructions written in a sequence
that achieves a goal

For complex problems software developers write an algorithm before


they attempt to write a computer program
Developing algorithms is a fundamental problem solving skill
◦ It has uses in many fields outside of Computer Science

CS121 - Introduction to Computer Applications and Programming 7


Good Program
What makes a good program?

❖ A program is a reflection of the writer and their thoughts


First, you must have some thoughts!
The difficulty for most people is to figure out what has to be done, write
the algorithm, before writing a program

CS121 - Introduction to Computer Applications and Programming 8


A Simple Example
A simple algorithm to get yourself a drink of orange juice
◦ For simplicity, the following are true:
◦ You have a clean glass in the cabinet
◦ You have orange juice in your refrigerator

So one valid algorithm is:


1. get a glass from your cabinet
2. go to the refrigerator and get the orange juice container
3. open the orange juice container
4. pour the orange juice from the container into the glass
5. put the orange juice container back in the refrigerator
6. drink your juice

CS121 - Introduction to Computer Applications and Programming 9


The Python Language
In the early 1990’s, Guido van Rossum designed what would become
the Python programming language
Van Rossum was dissatisfied with the languages available
◦ They were optimized to write large programs that executed quickly

He needed a language that could not only be used to create programs


quickly but also make them easy to modify
◦ It was designed to have a much simpler and cleaner syntax than other
popular languages such as Java, C and C++ (making it easier to learn)
◦ Python is interpreted, making it easier to develop and test short programs

Python programs are executed by the Python interpreter


◦ The interpreter reads your program and executes it
Python Running
Python
Source Code /
Interpreter
Code Output

CS121 - Introduction to Computer Applications and Programming 10


Computational Thinking
Having finished this course, we want you to have the following thought in
your subsequent college career.

“Hey, I’ll just write a program for that”. For us, that is “computational
thinking”

Python allows this to happen more readily.

CS121 - Introduction to Computer Applications and Programming 11


The Rules
1. Rule 1: Think before you program
➔ Determine the necessary steps to solve the problem.

2. Rule 2: A program is a human-readable essay on problem solving


that also happens to execute on a computer.
➔ Your program should be understandable by other people
(and you) so that it can be worked with, updated, improved, etc.

3. Rule 3: The best way to improve your programming and problem


solving skills is to practice.
➔ Problem solving and problem solving using
programming to record your solution requires practice.
So experiment. Try it, try again, see if you can fix it.

CS121 - Introduction to Computer Applications and Programming 12


Software
For the course, we will be using the Anaconda Python 3.11 Individual
distribution.

We recommend that you install the Anaconda Python 3.11 distribution using
the instructions found at the following link: Anaconda Python

Spyder IDE

.
.
This Photo by Unknown Author is
licensed under CC BY-SA-NC .

CS121 - Introduction to Computer Applications and Programming 13


First Commands
Step 1: Open the development environment. Type Spyder on
Windows Search Bar.
Step 2: Type the command(s).
Step 3: Evaluate the results.

Try the following:

CS121 - Introduction to Computer Applications and Programming 14


Things to Notice
Whitespace characters:
◦ Include spaces, newlines, tabs and they make programs and commands easier to read.
◦ Notice in the examples, we put spaces before and after the arithmetic operators ( 3 + 5 )
◦ This is an example of using whitespace to improve readability.
◦ The python interpreter ignores these extra characters, as they are for the reader of the
program (us!) rather than the interpreter.
◦ With the spaces removed, the program would behave in the same way.

CS121 - Introduction to Computer Applications and Programming 15


Things to Notice
Also notice that for multiplication, we use the * symbol instead
of 3 x 5.

These are examples of the syntax rules of python, which we will


discuss in detail next week.

CS121 - Introduction to Computer Applications and Programming 16


Anatomy of a Python Program
o Statements
o Comments
o Indentation

CS121 - Introduction to Computer Applications and Programming 17


print() Function
Python provides functions to carry out common tasks, the print
function is an example of this.
The print() statement allows us to print messages to the
console window in our programs.
print() can display text strings, numbers or calculations.
Text strings should be between single or double quotes.
If we would like to output multiple values, we can separate
them with a comma.

CS121 - Introduction to Computer Applications and Programming


18
Statements
A statement represents an action or a sequence of
actions.
The statement print("Welcome to Python")in the
program will display the greeting "Welcome to
Python".

Editor Window:
Console Window:
# Display a message
print("Welcome to Python")

CS121 - Introduction to Computer Applications and Programming 19


Analyzing Your First Program
A Python program contains one or more lines of instructions
(statements) that will be translated and executed by the
interpreter
# My first Python program
Print(“Welcome to Python”)

The first line is a comment (a statement that provides


descriptive information about the program to
programmers).
The second line contains a statement that prints a line of
text on screen: “Welcome to Python”

CS121 - Introduction to Computer Applications and Programming 20


Comments
A comment is an explanation in a program written to make
the code more readable by the programmers.

They are ignored by the Python interpreter (i.e. They are not
executed.)
# Display two messages
print("Welcome to Python")
print("Python is fun")
"""
This is a
multiple line comment
"""
CS121 - Introduction to Computer Applications and Programming
21
More Examples of the print
Function
Printing numerical values
print(3 + 4)
◦ Evaluates the expression 3 + 4 and displays 7

Passing multiple values to the function


print("The answer is", 6 * 7)
◦ Displays: The answer is 42
◦ Each value passed to the function is displayed, one after another, with a blank space after
each value

By default the print function starts a new line after its arguments are printed
print("Hello")
print("World!")
◦ Prints two lines of text:
Hello
World!

CS121 - Introduction to Computer Applications and Programming 22


Indentation
Indentation matters in Python.
Note that the statements are entered from the first
column in the new line. It would cause an error if
the program is typed as follows:
# Display two messages
print("Welcome to Python")
print("Python is fun")

• We will see in the upcoming weeks when we will need to use


indentation. For now, for regular program flow, we will align all our
statements to the left margin.

CS121 - Introduction to Computer Applications and Programming 23


Special Symbols

Character Name Description


() Opening and closing Used with functions or to group
parentheses arithmetic expressions.
# Pound sign Precedes a comment line.
' ' Opening and closing Enclosing a string (i.e. a sequence of
" " quotation marks (single or characters.)
double)
''' ''' Opening and closing Enclosing a multi-line paragraph
quotation marks (single or comment.
double)

CS121 - Introduction to Computer Applications and Programming 24


Printing Special Characters
Sometimes we want to include characters in our output that have
special meaning.
This includes characters such as quotes, newlines, etc.
The table below shows a number of special characters in python.
Special Meaning
Character
\n New line character
\t Tab character
\\ Backslash character
\' Single quote
\" Double quote

CS121 - Introduction to Computer Applications and Programming 25


Printing Special Characters -
Example
"Is this a cheese shop?"
'Yes'
"We have all kinds!“

print("\"Is this a cheese shop?\"\n\t'Yes'\n\t\"We have all kinds!\"")

CS121 - Introduction to Computer Applications and Programming 26


Variable
A variable is a name we designate to represent an object
(number, data structure, function, etc.) in our program.

We use names to make our program more readable, so that


the object is easily understood in the program.

CS121 - Introduction to Computer Applications and Programming


27
Variables
A variable is a named storage location in a computer program
There are many different types of variables, each type used to store
different things
You ‘define’ a variable by telling the compiler:
◦ What name you will use to refer to it
◦ The initial value of the variable

You use an assignment statement to place a value into a variable

CS121 - Introduction to Computer Applications and Programming 28


Variable Objects
Python maintains a list of pairs for every variable:
◦ variable's name
◦ variable's value

A variable is created when a value is assigned the first time. It


associates a name and a value
Subsequent assignments update the associated value.
We say name references value
Name Value
my_int = 7
my_int 7
Python name conventions
must begin with a letter or underscore _
◦ ab_123 is OK, but 123_abc is not.

may contain letters, digits, and underscores


◦ this_is_an_identifier_123

may be of any length

upper and lowercase letters are different (case sensitive)


◦ Length_Of_Rope is not length_of_rope

CS121 - Introduction to Computer Applications and Programming 30


= is assignment
In many computer languages, = means assignment.

my_int = my_int + 7
lhs = rhs
What assignment means is:

➢ evaluate the right hand side (rhs) of the =


➢ take the resulting value and associate it with the name on
the left hand side (lhs).

CS121 - Introduction to Computer Applications and Programming 31


More Assignment
Example:
my_var = 2 + 3 * 5

➢ evaluate expression (2+3*5): 17


➢ change the value of my_var to reference 17

Example (my_int has value 2):


my_int = my_int + 3

➢ evaluate expression (my_int + 3): 5


➢ change the value of my_int to reference 5

CS121 - Introduction to Computer Applications and Programming 32


Exercises
1. Write a program that will assign a value to a variable and display it on the monitor.
The variable should contain the value 10.
◦ Sample run:
◦ 10

2. Write a program to display:


◦ Sample run:
◦ We are the so-called "Vikings" from the north

3. Write a program to assign num1 to 8, num2 to 10 and then print :


o Sample run:
o Value of num1 is 8
o Value of num2 is 10

CS121 - Introduction to Computer Applications and Programming 33

You might also like