CS106 Week 02
CS106 Week 02
Introduction to
Computer kotak
Khan 🤣
By: Dr. kotak khan
Abasyn University (Khare Na Pursan)
03
Lecture
CS106-Introduction to Computer programming
tull Darogh waim
Introduction To
Programming
● Program: The term program refers to a set of
instructions that instructs a computer on what
to do. Programs are Solutions to Problems and
they are written using a programming
language.
● A program can instruct a computer to:
○ Read input data
○ Process the data
○ Store the data
○ Write or display the output
● Software: The term software refers to a
computer program or set of programs and its
associated documentation such as user guide,
technical manual
Why learn programming?
● Learning computer programming offers several valuable benefits,
regardless of your field. Here are 10 reasons why you should
consider diving into the world of computer programing:
● It is used for:
▪ web development (server-side),
▪ software development,
▪ mathematics,
▪ system scripting.
The Origins of Python
▪ The beginning of the Python language
started in December of 1989. The creator of
this language was Guido van Rossum who
began programming as more of a hobby. At
the time, van Rossum was working on a
project with the Dutch CWI research
institute, that was later terminated.
▪ Van Rossum was able to use some of the
basics of this new language, known as the
ABC language, in order to work on Python.
● Even if you have no prior programming experience, you should be able to get a
good start of Python introduction. We’ll take it slow, but before you know it,
you’ll have a solid base-level knowledge of the important topics:
▪ The Python REPL
▪ The print() function
▪ Variables
▪ Data Types
▪ Conditions
▪ Loops
▪ Iterators
▪ Functions
The Python REPL
● We’ll start our Python learning journey with the Python REPL,
which is short for Read-Evaluate-Print-Loop. It’s an interactive
shell that allows you to enter Python commands and directly see
the results.
● It’s a great way to tinker and learn! We’ll use the REPL as a
calculator and explore Python’s operators.
Exploring the Python REPL
(Shell)
●With your terminal open and the Python interactive shell started,
you’ll see a command prompt consisting of three arrows (>>>). To
be clear, you don’t type in the three arrows, only what follows after
it.
● Now type in print (“Hello World!”)
● >>> print (“Hello World!”)
● Hello World!
● >>> 10 + 10
● 20
>>> (4 + 6) * 12
>>> (8 - 2) / 3
>>> (3 * 3) + 3
>>> 2 + (9 - 4) + 5
>>> 4 * (5 + 3) - 4
>>> (4 + 5) - (2 + 7)
>>> 2 * ( 3+ 9) / 5
Value Assignment
Variable name
>>> number = 12
Variable value
>>> number
>>> 12
>>> calc = 2 + (9 - 4) + 5
>>> calc expression
>>> 12
Variable (nan chute da
wee)
● A variable has:
○ A name
○ A value
● To give (or assign) a value to a variable:
>>> variable = value
● We can also affect an expression
>>> variable = expression
Print variable value with
REPL
● To see the variable’s value with REPL
>>> variable
>>> value
>>> va = 42
>>> vb = 12 / 5
>>> va
42
>>> vb
2.40
04
Lecture
CS106-Introduction to Computer programming
Python Operators ( gap lagee
mara 🎈 )
01 02 03
Arithmetic Assignment Comparison
+, -, *, /, %, **, // =, +=, -=, *=, /=, ==, !=, <, >, <=, >=
%=, //=, **=
04 05 06
Logical Membership Bitwise
and, or, not in, not in &, |, ^, ~, <<, >>
sabaq ma waya tull gap de
>>> x + y >>> x % y
>>> 19 >>> 3
>>> x - y >>> x ** y
>>> 11 >>> 50625
>>> x * y >>> x // y
>>> 60 >>> 3
Python Comparison Operators
Less than or
> Greater than x>y <= x <= y
equal to
Comparison Operators:
Examples
>>> x = 15 >>> print (x < y)
>>> y = 4 >>> false
C Operato
o Description Example
r
m
pa and
Returns True if both statements are
x < 5 and x < 10
true
ri
so Returns True if one of the statements
or x < 5 or x < 4
n is true
Signed right
^ XOR x^y >> x >> 2
shift
Bitwise Operators: Examples
>>> print(6 & 3)
The & operator compares each bit and set it to 1 if both are 1,
otherwise it is set to 0:
6 = 0000000000000110
3 = 0000000000000011
====================
2 = 0000000000000010
====================
Bitwise Operators: Examples
>>> print(6 | 3)
6 = 0000000000000110
3 = 0000000000000011
====================
7 = 0000000000000111
====================
Bitwise Operators: Examples
>>> print(6 ^ 3)
The ^ operator compares each bit and set it to 1 if only one is 1,
otherwise (if both are 1 or both are 0) it is set to 0:
6 = 0000000000000110
3 = 0000000000000011
====================
5 = 0000000000000101
====================
Bitwise Operators: Examples
>>> print(3 << 2)
The << operator inserts the specified number of 0's (in this case 2)
from the right and let the same amount of leftmost bits fall off:
The >> operator moves each bit the specified number of times to
the right. Empty holes at the left are filled with 0's.
lagum)
Variable/Identifier
o Variables are containers for storing data values. It is a
memory (RAM) location to hold data (numbers &
strings).
o These are user defined words.
o Python has no command for declaring a variable.
o A variable is created the moment you first assign a value to it.
o After computation of an expression, its result can be stored in a variable
o A variable can later be used in the program.
o Initialization:
o with a constant (fixed value) using an Assignment operator
o with user desire value (By function)
Naming Rules for Identifier:
All identifier names must obey the following
rules:
● It is a sequence of characters that consists of letters (a-z), digits (0-9), &
underscore(_).
● must start with a letter or the underscore
● can be of any length
● cannot start with a number (digit)
● variable names are case-sensitive
● cannot be a Python keyword
● blank spaces are not allowed
● must be unique through out the program
● special symbols/characters (+, -, *, /, & etc) are not allowed
Valid/Invalid Identifiers
Task 1 Task 2 Task 3 Task 4 Task 4
Mercury a
Saturn+moo
Identifier Jupiter small
n
9_Neptune MOON
planet
Status
Special
Space is Starting with
Reasons No error
not allowed
character is
digit
not allowed
Quiz 1!
Write down 5 valid and 5 invalid identifiers
When Python detects an illegal identifier,
it reports a syntax error and terminates
execution of the program.
—Syntax Error
Errors
● Syntax Errors
○ The most common error you will encounter are syntax errors. Like any
programming language, Python has its own syntax, and you need to write code
that obeys the syntax rules. If your program violates the rules Python will report
syntax errors.
● Runtime Errors
○ Runtime errors are errors that cause a program to terminate abnormally. They
occur while a program is running if the Python interpreter detects an operation
that is impossible to carry out. Input mistakes typically cause runtime errors. An
input error occurs when the user enters a value that the program cannot handle.
● Logical/Semantic Errors
○ Logical errors occur when a program does not perform the way it was intended to.
Errors of this kind occur for many different reasons.
Python Assignment Operators