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

Python Programming 2021.Pptx

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

Python Programming 2021.Pptx

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

Python Programming

Syahrul Affendi bin Abdul Rahman


Maktab Rendah Sains Mara
Tun Ghazali Shafie
2021

1
BASIC PYTHON PROGRAM
INPUT/OUTPUT

2
Course Outcome
1. Identify basic Python element
2. Create basic Python program to display output
3. Identify basic data types
4. Declaring and initiate variable
5. Create user application with initialized values
6. Read user input
7. Create user application with user input

3
1. Identify basic Python element in Thonny

4
2. Create basic Python program to display output

5
TEST YOUSELF

6
3. Identify basic Data Types:
⚫ int – integer values (with No decimal)
⚫ Examples: 1, 200, 999
⚫ float – integer values (with decimal)
⚫ Examples: 10.1, 5555.0, 33.33
⚫ char – a character (text)
⚫ Examples: A, a, Z, z
⚫ String (word) – a set of characters (text)
⚫ Examples: Hello, Ali, Car Type
⚫ Boolean – represent one of two values either True or False
⚫ Examples: 1, 0, 2 > 1, 3 < 2

7
TEST YOURSELF
⚫ Identify the variables values below with correct data type:

int float char string Boolean


10 > 3
1000.0 Ahmad
9.99
a
0.10 A 1
.1 Z
Hi Blood 10
5<2
Type 8
4. Declaring and initiate variable
⚫ studentAge = int(10);

⚫ studentHeight = float(2.5);

⚫ studentBloodType = ‘A’;

⚫ studentName = “Syahrul”; OR studentName = ‘Syahrul’;

⚫ booleanValue = True

9
5. Create user application with initialized values
#include <iostream>
using namespace std;

int main()
{
int studenAge = 10;
cout<<“Display student age:”<< studentAge ;

system(“pause”);
return 0;
}

10
Create user application with
initialized values (STEPS)
STEPS:
1. Declare and initialize variable value
2. Display variable value

11
TEST YOURSELF

12
TEST YOURSELF

13
TEST YOURSELF

14
TEST YOURSELF

15
TEST YOURSELF

16
6. Read user input
⚫ studentAge = int(input("Please enter student age: "))

⚫ studentHeight = float(input("Please enter student height in meter: "))

⚫ studentBloodType = input("Please enter student blood type (ex: A, B, AB, O"): ")

⚫ studentName = input("Please enter student name: ")

17
7. Create user application with user input
studentAge = int(input("Please enter student age: "))
print(“Student age is " + str(studentAge) + " years old")

18
Create user application with user
input (STEPS)
STEPS:
1. Declare variable
2. Ask user input
3. Read user input
4. Display user input

19
TEST YOURSELF

20
TEST YOURSELF

21
TEST YOURSELF

22
TEST YOURSELF

23
TEST YOURSELF

24
APPLICATION

25
Example:
⚫ Write a program that calculate the area of rectangle

26
Solution

27
TEST YOURSELF
⚫ Write programs that calculate the area of following
shapes based on the formula given:
⚫ Rectangle
⚫ Triangle
⚫ Parallelogram
⚫ Trapezium
⚫ Circle

28
TEST YOURSELF
⚫ Write a program that calculates the volume of sphere
given that the value of radius is entered by user
through prompt. The formula to calculate the volume
of sphere as follows:

29
TEST YOURSELF
⚫ Write a program that calculates the kinetic Energy Ek
given that the value of mass (m) and velocity (v) are
entered by user through prompt. The formula to
calculate the kinetic energy as follows:

30
SELECTION

31
Course Outcome
⚫ Create Python program using if…else selection
statement

32
Create Python program using
if...else selection statement
print("1. Proton")
print("2. Perodua")
print("3. Ferrari")
print("4. Audi")
choice = int(input("Please choose your favorite car brand by inserting a number:
"))
if choice == 1:
print("My favourite car brand is Proton")
elif choice == 2:
print("My favourite car brand is Perodua")
elif choice == 3:
print("My favourite car brand is Ferrari")
elif choice == 4:
print("My favourite car brand is Audi")
else:
print("You didn't key in correct number")

33
TEST YOURSELF

34
TEST YOURSELF

35
APPLICATION

36
TEST YOURSELF
⚫ Write a program where a student can choose a diploma
program they like to enroll based on the menu below.
“You are not taking any Diploma Program” displayed if
otherwise input is entered
Menu Diploma Program
Z Diploma in Computer Science
W Diploma in Management Technology (Accounting)
F Diploma in Property Management
Q Diploma in Quantity Surveying
G Diploma in Management Technology

37
TEST YOURSELF
Write a program that display subject based on the input
entered by user based on the following table using
selection statement:
1. Account Principle
2. Biology
3. Art
“You are not taking any subject” displayed if otherwise
input is entered.

38
TEST YOURSELF
Write a program that display student club based on a
character entered by user. The program will display “You
are not taking any club” if otherwise. The option as
below:
C. Computer Club
A. Art Club
S. Science Club
L. Language Club

39
REPETITION

40
Course Outcome
⚫ Create Python program using for selection statement
⚫ Create Python program using do-while selection
statement
⚫ Create Python program using while selection
statement

41
Create Python program using for
selection statement
Syntax:
for variable in range (initial value, last value, increment/decrement)
print(variable)

42
Create Python program using for
selection statement (Example)

43
Create Python program using for
selection statement (Solution)
for x in range(0,6,1):
print(x)

44
TEST YOURSELF

45
TEST YOURSELF

46
Create Python program using while
selection statement
Syntax:
Variable = initial value
while variable < last value:
print(variable)
increment/decrement

47
Create Python program using while
selection statement (Example)

48
Create Python program using while
selection statement (Solution)
i=0
while i < 6:
print(i)
i += 1

49
TEST YOURSELF

50
TEST YOURSELF

51
END OF LESSON
THANK YOU (^_^)

52

You might also like