0% found this document useful (0 votes)
16 views55 pages

CS106 Week 02

The document serves as an introductory lecture on computer programming, specifically focusing on Python. It outlines the importance of learning programming, the history and features of Python, and provides insights into basic programming concepts such as variables, operators, and error types. The content is structured to guide beginners through the foundational aspects of Python programming, including syntax and practical examples.

Uploaded by

Ahsan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views55 pages

CS106 Week 02

The document serves as an introductory lecture on computer programming, specifically focusing on Python. It outlines the importance of learning programming, the history and features of Python, and provides insights into basic programming concepts such as variables, operators, and error types. The content is structured to guide beginners through the foundational aspects of Python programming, including syntax and practical examples.

Uploaded by

Ahsan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

CS(gup)-

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:

1. Problem-Solving Skills: Programming hones your ability to break


down complex problems into smaller, manageable steps. It’s like solving
puzzles, but with real-world applications.
2. Career Opportunities: Programming skills open up a wide range of
job prospects. From software development to data analysis, companies
across various industries seek professionals who can write code.
3. Creativity: Coding allows you to create something from scratch.
Whether it’s a website, an app, or a game, you get to express your
creativity through lines of code.
Why learn programming?
● 4. Automation: Programming lets you automate repetitive tasks.
Imagine writing a script to organize your files, send emails, or
perform other routine actions automatically.
● 5. Personal Development: Learning to code challenges your mind
and boosts cognitive abilities. It’s a lifelong skill that keeps you
intellectually engaged.
● 6. Understanding Technology: As our world becomes more digital,
understanding programming helps you navigate technology better.
You’ll know how software works and be better equipped to
troubleshoot issues.
● 7. Adaptability: Technology evolves rapidly. Learning programming
ensures you stay adaptable and relevant in a dynamic job market.
Why learn programming?
● 8. Collaboration: If you work with technical teams, knowing
programming helps you communicate effectively with developers,
designers, and IT professionals.
● 9. Innovation: Code drives innovation. Whether you’re building a
startup or enhancing existing systems, programming skills empower
you to create something new.
● 10. Fun and Fulfillment: Solving coding challenges, seeing your
projects come to life, and contributing to the digital world can be
incredibly rewarding.
History of Programming Languages
What is python?
● Python is a popular programming language. It was
created by Guido van Rossum, and released in 1991.

● 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.

▪ The following figure shows a global timeline of


Python’s historical and most defining releases:
Python 2 vs. Python 3

● As you can see, Python 2 and ● On top of that, Python 3 was


3 have been developed and initially slower than Python 2. As
maintained side by side for an Python 3 kept improving and
extended period. The primary receiving new features, it
reason is that Python 3 code is eventually took off. With the
not entirely backward latest efforts led by Guido, Python
compatible with Python 2 3 is now faster than ever. In
code. This incompatibility addition, Python 3 adds many
caused a prolonged adoption useful features to the language,
rate. Many people were happy making it easier and more fun.
with version 2 and had no
reason to upgrade.
Python Programs
● a program is a sequence of definitions and commands
○ definitions evaluated
○ commands executed by Python interpreter
in a shell
● commands (statements) instruct interpreter to do
something
● commands can be typed directly in a shell or stored in
a file that is read into the shell and evaluated
Introduction to Python
● You are now ready to start learning the Python programming. Make sure you
have Python installed on your system, or use an online version of 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!

● What happened? Remember we are in a REPL,


▪ Read: Python reads your command
▪ Evaluate: Python evaluates the string
▪ Print: it prints out what was evaluated
▪ Loop: it’s ready for the next input
Exploring The Python REPL
(Shell)
▪ Now type in 10 + 10:

● >>> 10 + 10
● 20

What happened? Remember we are in a REPL,


▪ Read: Python reads two numbers
▪ Evaluate: Python evaluates the two numbers to 20 using the
plus sign
▪ Print: it prints out what was evaluated
▪ Loop: it’s ready for the next input
Exploring The Python REPL (Shell)
▪ Now type in 5 + 8 * 4 and check the value being evaluated:
● >>> 5 + 8 * 4
● 37

+ and * are 5, 8 & 4 are


operators operand

For each code line


1. we give the expression 5+8* 4
2. an expression consists of the operands and operators.
3. shell evaluates the expression and returns the value
Expression
A valid expression gives a value
cddvdgsdgs Operators
+ Addition
- Subtraction
*
Multiplication
/ Division
>>> int / int
float
Operators and Parenthesis

>>> (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

A Operato Operato Exampl


ri Name Example Name
r r e
t
h + Addition x+y % Modulus x%y
m
et
- Subtraction x-y ** Exponentiation x ** y
ic
* Multiplication x*y // Floor division x // y
Arithmetic Operators:
Examples
>>> x = 15 >>> x / y
>>> y = 4 >>> 3.75

>>> x + y >>> x % y
>>> 19 >>> 3

>>> x - y >>> x ** y
>>> 11 >>> 50625

>>> x * y >>> x // y
>>> 60 >>> 3
Python Comparison Operators

C Operato Operato Exampl


o Name Example Name
r r e
m
p == Equal x == y < Less than x<y
a
ri Greater than or
!= Not Equal x != y >= x >= y
s equal to
o
n

Less than or
> Greater than x>y <= x <= y
equal to
Comparison Operators:
Examples
>>> x = 15 >>> print (x < y)
>>> y = 4 >>> false

>>> print (x == y) >>> print (x >= y)


>>> false >>> true

>>> print (x != y) >>> print (x <= y)


>>> true >>> false

>>> print (x > y)


>>> true
Python Logical Operators

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

Reverse the result, returns False if the


not not(x < 5 and x < 10)
result is true
Logical Operators: Examples
>>> x = 5
>>> print(x > 3 and x < 10)
>>> true
# returns True because 5 is greater than 3 AND 5 is less than 10

>>> print(x > 3 or x < 4)


>>> true
# returns True because one of the conditions are true (5 is greater
than 3, but 5 is not less than 4)

>>> print(not(x > 3 and x < 10))


>>> false
# returns False because not is used to reverse the result
Python Bitwise Operators

C Operato Operato Exampl


o Name Example Name
r r e
m
p & AND x&y ~ NOT ~
a
ri Zero fill left
| OR x|y << x << 2
s shift
o
n

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)

The | operator compares each bit and set it to 1 if one or both is 1,


otherwise it is set to 0:

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:

If you push 00 in from the left:


3 = 0000000000000011
becomes
12 = 0000000000001100
Bitwise Operators: Examples
>>> print(8 >> 2)

The >> operator moves each bit the specified number of times to
the right. Empty holes at the left are filled with 0's.

If you move each bit 2 times to the right, 8 becomes 2:


8 = 0000000000001000
becomes
2 = 0000000000000010
Variables ( 💋 ase gup
Also called Identifiers

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

A Exampl Exampl Same


s Operator e
Same as Operator
e as
si
g = x=6 x=6 /= x /= 5 x=x/5
n
m
+= x += 5 x=x+5 %= x %= 3 x=x%3
e
nt
-= x -= 9 x=x-9 //= x //= 2 x = x // 2
Python Assignment Operators ( just fun
😂)
A Exampl Exampl Same
s Operator e
Same as Operator
e as
si
g &= x &= 5 x=x&5 <<= <<= x = x << 5
n
m x= 5
|= |= x=x|5 := print( x:= 5)
e print (x)
nt
^= ^= x=x^5
Assignment
neshta 🤣
???

You might also like