You are on page 1of 21

DAY 2 PYTHON PROGRAMMING ESSENTIALS 1

DAY 2
PYTHON BASIC 2
PYTHON PROGRAMMING ESSENTIALS

Review on Day 1
DAY 1 PYTHON PROGRAMMING ESSENTIALS 2

Introduction
Installation of Python Interpreter
Familiarization on Code Editor and IDE Creating
first Python program Identifiers
Basic Syntax
Taking input/output
Output Formatting

Variables
DAY 2 PYTHON PROGRAMMING ESSENTIALS 3

Simply containers for storing values of data Python creates a


variable the moment we assign a value

# Declare a variable and initialize it area = 20


average = 90.65
status = “Pending”
initial = ‘J’

Datatype
DAY 2 PYTHON PROGRAMMING ESSENTIALS 4

Numeric int | float Binary bytes Boolean bool


Text str
Sequence list | tuple Mapping dict
Set set
print(f“The area is {area})

Type Conversion
DAY 2 PYTHON PROGRAMMING ESSENTIALS 5

 Converting one data type into another. Use to specify type of data on a variable
#converting numeric to text
area = 20

#converting text to numeric


num1 = int(input(“Enter num1: “))
num2 = int(input(“Enter num2: “))
print(num1 + num2)

value1 = input(“Enter number1: “)


value2 = input(“Enter number2: “)
print(float(value1) + float(value2))

Operators & Expressions DAY 2 PYTHON PROGRAMMING ESSENTIALS 6

Operators are divided into the following groups 


Arithmetic Operators
 Relational Operators
 Assignment Operators
 Logical Operators
Addition + 15 + 2 = 17 Subtraction - 15 - 2 = 13
Multiplication * 15 * 2 = 30 Division / 15 / 2 = 7.5
Modulus % 15 % 2 = 1 Exponentiation ** 15 ** 2 = 225
Floor division // 15 // 2 = 7

Arithmetic Operators
DAY 2 PYTHON PROGRAMMING ESSENTIALS 7

Equal ==
Not Equal !=
Greater than >
Less than <
Less than or equal <=
Relational Operators
DAY 2 PYTHON PROGRAMMING ESSENTIALS 8 Greater than or equal to >=
Equal sign = x = 2 Add AND += x += 3 equivalent x = x +
3Subtract AND -= x -= 2 equivalent x = x – 3Multiply AND
*= x *= 2 equivalent x = x * 3Divide AND /= x /= 2 equivalent
x = x / 2Modulus AND %= x %=2 equivalent x = x % 2

Assignment Operators
DAY 2 PYTHON PROGRAMMING ESSENTIALS 9
x = True y = False
AND and x and y = False OR or x or y = True NOT not
not x = False

Logical Operators
DAY 2 PYTHON PROGRAMMING ESSENTIALS 10

Parenthesis ( )
Exponent **
MDMod * / % AS + -

Precedence of Operators
DAY 2 PYTHON PROGRAMMING ESSENTIALS 11
1. Allow the user to enter the following
1. Name (Text) (Peter Smith) 2. Math Grade (float) (90.34) 3.
Science Grade (float) (85.32)
4. English Grade (integer) (80) DAY 2 PYTHON PROGRAMMING ESSENTIALS 12

2. Compute the average (float)


3. Display the appropriate values
Hi! Peter Smith
Math grade : 90.34
Science grade : 85.32
English grade: 80
Your average grade is 85.22

Practice 1
"""Day 2 – Practice 1
By: Rogie Taborda
"""
DAY 2 PYTHON PROGRAMMING ESSENTIALS 13

name = str(input("Enter name: "))


math = float(input("Enter math grade: "))
science = float(input("Enter science grade: ")) english =
int(input("Enter english grade: ")) average = (math + science +
english) / 3 print(f"Hi! {name}")
print(f"Math grade: {math}")
print(f"Science grade: {science}")
print(f"English grade: {english}")
print(f"Your average grade is {average}")
1. Allow the user to enter a positive integer then display
the number of 2. Example: 1561
1000 - 1
500 - 1
100 - 0
50 - 1
20 - 0
10 - 1
5-0
1-1
denominations.
DAY 2 PYTHON PROGRAMMING ESSENTIALS 14

Exercise 1
Filename: Day2_Exe1_Lastname_Firstname.py

String
DAY 2 PYTHON PROGRAMMING ESSENTIALS 15
A string is a sequence of characters.
Python does not have a character data type Square
brackets can be used to access elements of the string. string1 =
“The quick brown fox” string2 = ‘The fast green turtle’ string3 = ”””This milk
costs 30 pesos ”””

Accessing Characters DAY 2 PYTHON PROGRAMMING ESSENTIALS 16

Single characters on a string can be accessed by indexing.

string1 = "The quick brown fox"

# print first character


print(string1[0])
# print last character
print(string1[-1])
# print range from index 1 to 6th characterprint(string1[1:6]) #string
slicing

String Manipulations DAY 2 PYTHON PROGRAMMING ESSENTIALS 17

Concatenation of two strings


str1 = “Python is”
str2 = “the best!”
str3 = str1 + str2
print(str3)

Iteration using loop


Changing & deleting single character is not allowed in a
stringUpdating & delete a string is allowed Use
del keyword to delete a string
Escape Sequence
DAY 2 PYTHON PROGRAMMING ESSENTIALS 18

To display single or double quotes


 What’s going on?
str1 = “What\’s going on?”  C:\Python\Python.exe

str2 = “C:\\Python\\Python.exe”  What is \“ngarod”\?


str3 = "What is \\\"ngarod\"\\?"

String Functions
DAY 2 PYTHON PROGRAMMING ESSENTIALS 19
Returns string as uppercase upper( )Returns string as
lowercase lower( )Converts first character to capital letter
capitalize( )Every first letter of each word capitalized title(
)Splits the string at the specified separator split( )Returns
a string where value is replaced replace( )Returns number of
characters on a string len( )

String Functions
DAY 2 PYTHON PROGRAMMING ESSENTIALS 20

str1 = “python is everywhere!”


print(str1.lower())
print(str1.upper())
print(str1.capitalize()) print(str1.title())
print(str1.split())
print(str1.replace(“python”, “java”) print(len(str1))
print(str1.find(‘e’))
1. Let the user enter a text and a valid number – n (less
than the 2. Move the last n characters at the beginning

of the text 3. Display the new text similar to given


below. New text: AMMINGpython progr
Paractice 2
length of the text)
DAY 2 PYTHON PROGRAMMING ESSENTIALS 21

Enter text: python programming


Enter number: 6
val2 = 4.32

Numbers
DAY 2 PYTHON PROGRAMMING ESSENTIALS 22

Number data types store numeric values


They are immutable data types, meaning changing the value
results in a newly allocated object
val1 = 3

print(type(val1))
print(type(val2))
Mathematical Functions DAY 2 PYTHON PROGRAMMING ESSENTIALS 23

We need to include math library on the program –


importmathmath.ceil()
math.floor()
math.pow(2,3)
math.sqrt(20)
round(5.1624, 2)

Mathematical Functions DAY 2 PYTHON PROGRAMMING ESSENTIALS 24

We need to include math library on the program –


importmathmath.pi
math.cos(23)
math.exp(3)
math.log10(432)
math.sinh(3)
math.factorial(10)

Random Number
DAY 2 PYTHON PROGRAMMING ESSENTIALS 25

We need to include random library on the


programimport random
rnd1 = random.random()
rnd2 = random .randrange(1, 11)
1. Create a program that will
2. Compute the prize by 3. The program shall

----- DRAW RESULTS-----


Enter your name: Will SmithEnter bet amount:
4. Display the result.
12000Enter your lucky numbers:
6 16 22 28 38 42
let the user enter name,
bet amount and 6
different numbers from 1
to 49
DAY 2 PYTHON PROGRAMMING ESSENTIALS 26

multiplier mechanism (Random numbers. No duplicate


x2-5) numbers.
Name: Will Smith Amount: 12000
generate 6 random winning Multiplier x4 Prize: 48000 Entry No's:
6 16 22 28 38 42 47

* D R A W R E S U L T *9 5 37 17 26

Exercise 2
Filename: Day2_Exe2_Lastname_Firstname.py

Summary
DAY 2 PYTHON PROGRAMMING ESSENTIALS 27

Variables
Datatype
Type Conversion
Operators & Expressions
String
Numbers
Random

You might also like