3 Python Basics Part 2
3 Python Basics Part 2
3 Python Basics Part 2
Lesson 2
Outline:
1. Input/Print Function
2. Output Formatting
3. Import Function
4. String Operations
5. List Methods
6. Slicing
7. Operators and Basic Mathematical Function
8. Built-in Functions
Input/Output (I/O)
<variable> = input(<message>)
Example:
value = input("Enter a value: ")
Input Function
Example:
number = input(“Enter a number: “)
Enter a number: 10
10
Example:
value = input("Enter a value: ")
print(value)
Output Formatting
For example:
x = 5; y = 10
print(“The value of x is {} and y is {}”.format(x,y))
Output:
The value of x is 5 and y is 10.
Output:
I love bread and butter
I love butter and bread
Result:
Hello John, Handsome
Output Formatting
Example:
x=5
y = 10
print(f“The value of x is {x} and y is {y}”)
import math
pi = math.pi
print(f"The value of pi is {pi}.")
result = math.factorial(5)
print(f"5 factorial: {result}")
Import in Python
import math
pi = math.pi
print("The value of pi is", pi)
result = math.factorial(5)
print("5 factorial: ", result)
Import in Python
Example:
from math import pi
pi
Import in Python
Example:
from statistics import mean, median
numbers = [1, 2, 3, 4, 5]
average = mean(numbers)
middle = median(numbers)
Math Module:
https://docs.python.org/3/library/math.html
String Operations
String Operations
What is Slicing?
Example:
a=2+3
Output: 5
Arithmetic operators
Arithmetic operators are used to perform
mathematical operations like addition, subtraction,
multiplication, etc.
Arithmetic operators
Arithmetic operators
Note: we can also use this:
Example x = 15
y = 4 a = x + y
print(f"x + y = {a}")
print('x + y =',x+y)
# Output: x + y = 19
print('x - y =',x-y)
# Output: x - y = 11
print('x * y =',x*y)
# Output: x * y = 60
Arithmetic operators
Example x = 15
y = 4
print('x / y =',x/y)
# Output: x / y = 3.75
print('x // y =',x//y)
# Output: x // y = 3
print('x ** y =',x**y)
# Output: x ** y = 50625
Arithmetic operators
Increment
age = 5
age += 1 # increment
print(age)
Output: 6
Comparison operators
Comparison operators are used to compare values.
It returns either True or False according to the
condition.
Comparison operators
Comparison operators
Note: we can also use this:
Example x = 10
y = 12 a = x > y
print(f"x > y = {a}")
# Output: x > y is False
print('x > y is',x>y)
# Output: x == y is False
print('x == y is',x==y)
Comparison operators
Example x = 10
y = 12
# Output: x != y is True
print('x != y is',x!=y)
https://www.geeksforgeeks.org/python-operators/
Sample Program
1.Compute the area of a triangle given
the length of its three sides: a,b,c
with formulas:
Sample Program NOTE:
In the str.format method, the
import math last placeholder has a value
of :0.2f, was used to format
a = int(input ('Side a: ')) the number of decimal places
b = int(input ('Side b: ')) to be displayed. It follows this
c = int(input ('Side c: ')) syntax:
{<index>:<format-specifier>}
s = (a + b + c) / 2
A = math.sqrt(s*(s-a) * (s-b) * (s-c))
A = w * l
P = 2 * (w + l)
A = w * l
P = 2 * (w + l)