Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Revision Day 5

Uploaded by

mufeedsheik786
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Revision Day 5

Uploaded by

mufeedsheik786
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Final Day

• Python Revision
• AI ML model Live Building
• AI ML concepts Revision
• Till 11
• 11 30 Animation using scratch
• 11 30 to 12 30 Basic Website Building
• Basics of ethical hacking
• Send Off
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)
• String constants use single quotes (') 123
or double quotes (") >>> print(98.6)
98.6
>>> 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”

• Programmers get to choose the names of the variables

• You can change the contents of a variable in a later statement

x = 12.2 x 12.2
y = 14
y 14
Reserved Words
You cannot use reserved words as variable names / identifiers

False await else import pass


None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
Python Variable Name Rules
• Must start with a letter or underscore _

• Must consist of letters, numbers, and underscores

• Case Sensitive

Good: spam eggs spam23 _speed


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

hours = 35.0
What are these rate = 12.50
bits of code doing? pay = hours * rate
print(pay)
Sentences or Lines

x = 2 Assignment statement
print(x) Print statement

Variable Operator Constant Function


Expressions…
Arithmetic/ Numeric Operators
Operator Operation
• Because of the lack of mathematical
symbols on computer keyboards - we + Addition
use “computer-speak” to express the - Subtraction
classic math operations
* Multiplication
• Asterisk is multiplication / Division

• Exponentiation (raise to a power) looks ** Power


different than in math % Remainder
Operator Precedence Rules
Highest precedence rule to lowest precedence rule:

• Parentheses are always respected Parenthesis


Power
• Exponentiation (raise to a power) Mult / Div / Remainder
Add, Sub
• Multiplication, Division, and Remainder
Left to Right
• Addition and Subtraction

• Left to right
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11.0 1 + 8 / 4 * 5
>>>
1 + 2 * 5
Parenthesis
Power
Multiplication 1 + 10
Addition
Left to Right
11
xx = 1
type (xx)
Several Types of Numbers
<class 'int’>

• Numbers have two main types temp = 98.6


type(temp)
- Integers are whole numbers:
-14, -2, 0, 1, 100, 401233 <class'float’>

- Floating Point Numbers have type(1)


decimal parts: -2.5 , 0.0, 98.6, 14.0
<class 'int’>
• There are other number types - they
are variations on float and integer type(1.0)

<class'float'>
User Input
• We can instruct Python to
pause and read data name = input('Who are you? ')
print('Welcome', name)
from the user using the
input() function

• The input() function


Output Terminal
returns a string
Who are you? Chuck
Welcome Chuck
Comments in Python
• Anything after a # is ignored by Python

• Why comment?

- Describe what is going to happen in a sequence of code

- Document who wrote the code or other ancillary information

- Turn off a line of code - perhaps temporarily


#python will not read the code starting with #

name = input('Who are you? ')


print('Welcome', name)
Comparison Operators
• Boolean expressions ask a Python Meaning
question and produce a Yes or No < Less than
result which we use to control
program flow <= Less than or Equal to
== Equal to
• Boolean expressions using >= Greater than or Equal to
comparison operators evaluate to
> Greater than
True / False or Yes / No
!= Not equal
• Comparison operators look at
variables but do not change the Remember: “=” is used for assignment.
variables
http://en.wikipedia.org/wiki/George_Boole
Empty Lines between code can be ledt but its not a good
coding practice
One-Way Decisions
Yes
x == 5 ?
x = 5
if x == 5 : No print('Is 5’)
print('Is 5')
print('Is Still 5')
print('Third 5') print('Still 5')
if x == 6 :
print('Is 6') print('Third 5')
print('Is Still 6')
print('Third 6')
Think About begin/end Blocks
x = 5
if x > 2 :
print('Bigger than 2')
print('Still bigger')
print('Done with 2')

for i in range(5) :
print(i)
if i > 2 :
print('Bigger than 2')
print('Done with i', i)
print('All Done')
Visualize Blocks x=4

no yes
x = 4 x>2

if x > 2 :
print('Bigger') print('Not bigger') print('Bigger')
else :
print('Smaller')

print('All done')
print('All Done')
Multi-way
yes
print('small
x<2 number')
no
if x < 2 :
yes print('Medium
print('small number')
x < 10 number')
elif x < 10 :
print('Medium number') no
else : print(‘Large
print(' Large number ') number')
print('All done')

print('All Done')
n=5 An Infinite Loop
No Yes
n>0?
n = 5
print('Lather') while n > 0 :
print('Lather')
print('Rinse’)
print('Rinse')

print('Dry off!') What is wrong with this loop?


• The break statement ends the current loop and jumps to the
Breaking Out of
statement immediately following the loopa Loop
• It is like a loop test that can happen anywhere in the body of the
loop > hello there
while True:
line = input('> ') hello there
if line == ‘im done' : > finished
break finished
print(line) > im done
print(‘im Done bye!!!!!') im Done bye!!!!!
A Simple Definite Loop

for i in [5, 4, 3, 2, 1] : 5
print(i) 4
3
print('Blastoff!')
2
1
Blastoff!
A Simple Definite Loop
5
for i in [5, 4, 3, 2, “Hello”] :
print(i)
4
print('Blastoff!') 3
2
Hello
Blastoff!
A Simple Definite Loop

for i in [“Hi”, “a”, “b”, “c”, “Hello”] : Hi


print(i) a
print('Blastoff!')
b
c
Hello
Blastoff!
Python Functions
• There are two kinds of functions in Python.

- Built-in functions that are provided as part of Python - print(),


input(), type(), float(), int() ...

- Functions that we define ourselves and then use

• We treat function names as “new” reserved words


(i.e., we avoid them as variable names)
x = 5
print('Hello')

def lyrics():
print("I'm a lumberjack, and I'm okay.")
print('I sleep all night and I work all day.')

print('Yo')
lyrics()
Hello
x = x + 2
print(x) Yo
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
7
Return Values
Often a function will take its arguments, do some computation, and
return a value to be used as the value of the function call in the
calling expression. The return keyword is used for this.

def greet():
return "Hello" Hello Glenn
Hello Sally
print(greet(), "Glenn")
print(greet(), "Sally")
Multiple Parameters / Arguments
• We can define more than one
parameter in the function def addtwo(a, b):
definition added = a + b
return added
• We simply add more arguments
x = addtwo(3, 5)
when we call the function print(x)

• We match the number and order 8


of arguments and parameters

You might also like