You are on page 1of 34

PYTHON

PROGRAMMING
WHAT DO YOU KNOW
ABOUT PROGRAMMING
LANGUAGES?
PROGRAMMING LANGUAGE

• Set of rules to perform certain tasks in your


computer

Instructions Output
Process your
instructions
Example: 2+3 Example: 5

CPU
PROCESS

Instructions
CPU
Example: 2+3

Output
Main Memory
Example: 2+3
Python is initially developed by
Guido van Rossum.
PYTHON
WHY PYTHON?

• Simple syntax (the structure of programming language)


• Applied in many fields
INSTALLING PYTHON

• Python.org
FIRST PROGRAM

>>> print(“Hello World!”)


Output:
Hello World!
INTERACTIVE VS SCRIPT

IDLE Script
EXECUTING SCRIPT

Step 1 : Save the script file with .py extension


Step 2 : Open your command prompt
Step 3 : Change the directory to the location where
the file is stored
Step 4 : use the command “python file_name.py”
SENTENCES OR LINES

Program: print(z)
x=2
y=3 Output:
z=x+y 5
ERROR
Unable to execute your
instructions
VARIABLES,
EXPRESSIONS AND
STATEMENTS
CONSTANTS
• Fixed values such as numbers, letters, and
strings, are called “constants” because their
value does not change

• Numeric constants are as you expect >>> print(123)


123
>>> print(98.6)
• String constants use single quotes (') 98.6
or double quotes (") >>> print('Hello world')
Hello world
VARIABLES
• A variable is a named place in the memory
where a programmer can store data and later
retrieve the data using the variable “name” x = 12.2
• Programmers get to choose the names of the y = 14
variables

• You can change the contents of a variable in a


later statement
STORAGE OF VARIABLES IN
MEMORY
1001 x
1002 y
x 12.2 1003 …
1004 …

y 14 1005 …
1006 …
1007 …
1008 …
1009 …
.
.
.
.
…….
VARIABLES

x = 12.2 x 12.2 100


y = 14
x = 100 y 14
RULES FOR VARIABLES NAMES

• Must start with a letter or underscore ( _ )


• Must consist of only letters, numbers, and
underscores
• Case Sensitive

Good: spam eggs spam23 _speed


Bad: 23spam #sign var.12
Different: spam Spam SPAM
MNEMONIC VARIABLE NAMES
• Since we programmers are given a choice in how we choose our variable
names, there is a bit of “best practice”
• We name variables to help us remember what we intend to store in them
(“mnemonic” = “memory aid”)
• This can confuse beginning students because well-named variables often
“sound” so good that they must be keywords

Good: spam eggs spam23 _speed


Bad: 23spam #sign var.12
Different: spam Spam SPAM
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print(x1q3p9afd)

What is these bits of code


doing???
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
c=a*b
print(x1q3p9afd)
print(c)

What is these bits of code


doing???
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
c=a*b
print(x1q3p9afd)
print(c)

What is these bits of code hours = 35.0


doing??? rate = 12.50
pay = hours * rate
print(pay)
RESERVED WORDS
You cannot use reserved words as variable names /
identifiers
False class return is finally
None if for lambda continue
True def from whilenonlocal
and del global not with
as elif try or yield
assert else import pass
break except in raise
SENTENCES OR LINES

x=2 Assignment statement


x=x+2 Assignment with expression
print(x) Print statement

Variable Operator Constant Function


ASSIGNMENT STATEMENTS
• We assign a value to a variable using the assignment
statement (=)

• An assignment statement consists of an expression on


the right-hand side and a variable to store the result

x = 3.9 * x * ( 1 - x )
A variable is a memory location
used to store a value (0.6)
x 0.6
0.6 0.6
x = 3.9 * x * ( 1 - x )

0.4
The right side is an expression.
Once the expression is evaluated, the result 0.936
is placed in (assigned to) x.
A variable is a memory location used
to store a value. The value stored in a
variable can be updated by replacing
the old value (0.6) with a new value x 0.6 0.936
(0.936).

0.936 0.936
x = 3.9 * x * ( 1 - x )

0.064
The right side is an expression.
Once the expression is evaluated, the result 0.2336256
is placed in (assigned to) the variable on left-
side ( i.e. x.)
EXPRESSIONS…
NUMERICAL EXPRESSIONS
Operator Operation
• Because of the lack of mathematical
symbols on computer keyboards - we + Addition
use similar symbols to express the
classic math operations - Subtraction

* Multiplication
• Asterisk is multiplication
/ Division
• Exponentiation (raise to a power) looks
different than in math ** Power

% Remainder
NUMERICAL EXPRESSIONS
>>> xx = 2 >>> jj = 23
>>> xx = xx + 2 >>> kk = jj % 5
>>> print(xx) >>> print(kk)
4 3
>>> yy = 440 * 12 >>> print(4 ** 3)
>>> print(yy) 64
5280 4R3
>>> zz = yy / 1000
>>> print(zz) 5 23
5.28 20
3
ORDER OF EVALUATION
• When we string operators together -
Python must know which one to do first

• This is called “operator precedence”

• Which operator “takes precedence” over


the others?

x = 1 + 2 * 3 - 4 / 5 ** 6
OPERATOR PRECEDENCE RULES
Highest precedence rule to lowest precedence rule:

• Parentheses are always respected Parenthesis


Power
• Exponentiation (raise to a power) Multiplication
• Multiplication, Division, and Remainder Addition
Left to Right
• Addition and Subtraction

• Left to right
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x) 1 + 2 ** 3 / 4 * 5
11.0
>>> 1+8/4*5

Parenthesis
Power 1+2*5
Division, Multiplication
Addition, Subtraction 1 + 10
Left to Right

11

You might also like