0% found this document useful (0 votes)
45 views21 pages

CSE100 wk3 4

This document provides an overview of programming with Python. It discusses objectives of computer science and programming languages. It also covers basic Python programming concepts like variables, data types, expressions, control flow, functions and classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views21 pages

CSE100 wk3 4

This document provides an overview of programming with Python. It discusses objectives of computer science and programming languages. It also covers basic Python programming concepts like variables, data types, expressions, control flow, functions and classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Programming with Python

Dr. Mohammed Eunus Ali


([email protected])

Department of Computer Science and Engineering


Bangladesh University of Engineering and Technology (BUET)
Dhaka-1000, Bangladesh
Objectives

Computer and Computer Science

Programming Languages

Programming with Python

Text Books:
1. Allen B. Downey, Jeffrey Elkner and Chris Meyers:
How to Think Like a Computer Scientist: Learning with Python.
2. Guido van Rossum, the inventor of Python, and Fred L. Drake,
Jr., the official editor of the Python documentation, wrote a
tutorial introduction to Python.
2
Natural vs. Formal Languages

• Natural languages:
– The languages that people speak, such as Bengali, English, and
French. They were not designed by people (although people try to
impose some order on them); they evolved naturally
• Formal languages:
– The languages that people have designed for specific purposes,
such as representing mathematical ideas or computer programs; all
programming languages are formal languages
• Token:
– One of the basic elements of the syntactic structure of a program,
analogous to a word in a natural language
• Parse:
– To examine a program and analyze the syntactic structure
3
Programming Languages

• Programming languages are formal languages that


have been designed to express computations

– Natural language has ambiguity and imprecision problems when


used to describe complex algorithms
– Programs expressed in an unambiguous , precise way using
programming languages
– Every structure in programming language has a precise form,
called its syntax
– Every structure in programming language has a precise meaning,
called its semantics

4
Programming Languages

• High-level programming languages


– Designed to be understood by human
• Low-level programming languages
– Designed to be understood by machines (also known as machine
languages or assembly languages)
• High-level vs. Low-level
– It is much easier to program in a high-level language. Programs
written in a high-level language take less time to write, they are
shorter and easier to read, and they are more likely to be correct
– High-level languages are portable, meaning that they can run on
different kinds of computers with few or no modifications. Low-
level programs can run on only one kind of computer and have to
be rewritten to run on another

5
Programming Languages

• High-level to low-level
– Computers can only execute programs written in low-level
languages. Thus, programs written in a high-level language have
to be processed before they can run.
– Two kinds of programs process high-level languages into low-level
languages: interpreters and compiler.
• Interpreter vs Compiler
– An interpreter reads a high-level program and executes it,
meaning that it does what the program says.
– A compiler reads the program and translates it completely before
the program starts running. In this case, the high-level program is
called the source code, and the translated program is called the
object code or the executable.

6
Why Python!!!

• Programming in Python is simply a lot of fun


and more productive
• Python is easier to understand

7
The First Program in Python

• print "Hello, World! Welcome to CSE“

8
Basic Instructions

• Input:
– Get data from the keyboard, a file, or some other device
• Output:
– Display data on the screen or send data to a file or other device
• Math:
– Perform basic mathematical operations like addition and
multiplication
• Conditional execution:
– Check for certain conditions and execute the appropriate
sequence of statements
• Repetition:
– Perform some action repeatedly, usually with some variation

9
Debugging
• Errors:
– Programming is a complex process, and because it is done by human
beings, it often leads to errors
– Programming errors are called bugs and the process of tracking them
down and correcting them is called debugging
– Different types: syntax errors, runtime errors, semantic errors
– Syntax Errors:
• An error in the syntax (i.e., the structure) a program that makes it
impossible to parse (and therefore impossible to interpret)
– Runtime Errors:
• An error that does not occur until the program has started to execute but
that prevents the program from continuing
– Semantic Errors:
• An error in a program that makes it do something other than what the
programmer intended

10
Variables
• Variables are containers for storing information
• Variables are designed to hold specific types of
information
• Names
– Names are given to variables, functions, modules, etc.
– These names are called identifiers
– Every identifier must begin with a letter or underscore (“_”), followed
by any sequence of letters, digits, or underscores
– Identifiers are case sensitive
– Some identifiers are part of programming language itself. These
identifiers are known as reserved words. This means they are not
available for you to use as a name for a variable, etc. in your program

11
Types

• Boolean:
– Variables of this type can be either True or False
• Integer:
– An integer is a number without a fractional part, e.g. -4, 5, 0, -
3
• Float:
– Any rational number, e.g. 3.432
• String:
– Any sequence of characters

12
Variables

• No need to declare
• Need to assign (initialize)
– use of uninitialized variable raises exception
• Not typed
if friendly:
greeting = "hello world"
else:
greeting = 12**2
print greeting
• Everything is a "variable":
– Even functions, classes, modules

13
Expressions
• Expression: A data value or set of operations to compute a value.
Examples: 1+4*3
42
• Arithmetic operators we will use:
– +-*/ addition, subtraction/negation, multiplication,
division
– % modulus, a.k.a. remainder
– ** exponentiation
– // floor

14
Order of Operations

• When more than one operator appears in an expression,


the order of evaluation depends on the rules of
precedence.
• Python follows the same precedence rules for its
mathematical operators that mathematics does, i.e,
PEMDAS.
• Multiplication and Division has the same precedence.
• Addition and Subtraction has the same precedence.
• Operators with the same precedence are evaluated from
left to right.
• E.G. 3*1**3=?, 2/3-1=?

15
Statement

• A statement is an instruction that the Python


interpreter can execute
– E.g.: print and assignment
• A program/script usually contains a sequence of
statements
• Composition: The ability to combine simple expressions
and statements into compound statements and
expressions in order to represent complex computations
concisely

16
Assignment
• Simple Assignment
– <variable> = <expr>
variable is an identifier, expr is an expression
• The expression on the RHS is evaluated to produce a
value which is then associated with the variable
named on the LHS
• Variables can be reassigned as many times as you
want
– cgpa = 3.8
– cgpa = 3.75
• Simultaneous Assignment
– Several values can be calculated at the same time
– <var>, <var>, … = <expr>, <expr>, …

17
Input and Output
• Input: The purpose of an input statement is to get input
from the user and store it into a variable:
– <variable> = input(<prompt>)
– E.g., x = input(“Enter a temperature in Celsius: ”)
• Output: Output Statements
– A print() function can print any number of expressions
– Successive print statements will display on separate lines
– A bare print will print a blank line ( example: print() )
– If a print statement ends with a “,”, the cursor is not advanced to the
next line (not applicable in python 3.4)
– Examples:
print (3, 4, 3+4) 3,4,7
print (3, 4,) 3,4,7
print (3+ 4)
print “The answer is”, 3+4 The answer is 7

18
Assignment

• Assignment manipulates references


– x = y does not make a copy of y
– x = y makes x reference the object y references
• Very useful; but beware!

• a=1 a 1
New int object created by
add operator 1+1
• b=a 1
a
b
• a 2 old reference deleted
by assignment (a=...)
• a = a+1 b 1
19
Assignment Operators
• = Simple assignment operator, Assigns values from right side
operands to left side operand c = a + b will assign value of a + b into c
• += Add AND assignment operator, It adds right operand to the left
operand and assign the result to left operand c += a is equivalent to c
=c+a
• Similarly,
– -= Subtract AND assignment operator
– *= Multiply AND assignment operator
– /= Divide AND assignment operator
– %= Modulus AND assignment operator
– **= Exponent AND assignment operator
– //= Floor Division and assigns a value

20
Bitwise Operators
• & : Binary AND Operator copies a bit to the result if it exists in both
operands. (a & b) will give 12 which is 0000 1100
• | : Binary OR Operator copies a bit if it exists in either operand. (a | b) will
give 61 which is 0011 1101
• ^ : Binary XOR Operator copies the bit if it is set in one operand but not
both. (a ^ b) will give 49 which is 0011 0001
• ~ : Binary Ones Complement Operator is unary and has the effect of
'flipping' bits. (~a ) will give -60 which is 1100 0011
• << : Binary Left Shift Operator. The left operands value is moved left by the
number of bits specified by the right operand. a << 2 will give 240 which is
1111 0000
• >> : Binary Right Shift Operator. The left operands value is moved right by
the number of bits specified by the right operand. a >> 2 will give 15 which
is 0000 1111
a  0011 1100 b  0000 1101
21

You might also like