Learn Python
Learn Python
INTRO
String is series of character
String symbols is “” or ‘’
Expression : piece of code that produces value
===========================================================
VARIABLES
We use variable to store temporary data/like a box
Example : price = 10 it’s mean box labelled ‘price’ contains number 10
List of Variables :
*when defining the variables, always use lowercase while special case (contain) use uppercase
================================================================
SIMPLE VALUES
1. price = integers (without decimals) 10
2. rating = float (for decimals) 4.9
3. name = ‘mohd’
4. is_published = Boolean (true/False)
=============================================================
COMPLEX VALUES
==================================================
RECEIVING INPUT
Receive input from the user
Command : input()
Function input() it will print the MESSAGE and wait for the user to enter the VALUE
Example :
name = input(‘What is your name? ‘)
print(‘Hi’ + name)
When it print, it will see “ What is Ypur name?”
Then user put the name after the question example : ‘Ahmad’
Then it wil print out : “Hi Ahmad”
================================================================
TYPE CONVERSION
Input function always get string. You can convert the string into integers or float or others
Examples : we will write program tha will ask the year of born and then it would calculate the age
birth_year = input(‘Birth year: ‘)
age = 2009 – int(birth_year) we add int to convert string to int to make substraction
print(age)
it will calculate and print 2009 – [what numbers user enter]
===============================================================
STRING
We can choose to use single, double or triple quote to type string
Single quote :
course : ‘Hello My Name is Ahmad’
Double quote :
course : “Hello My Name is Ahmad”
Triple quote ( usually for type message an email)
We use : ‘‘‘ (to start) ‘’’ (to terminate/close) use for multi-line string
Example :
Course : ‘’’
Hi Ahmad
Thanks for subscribing our channel
‘’’
Print(course)
===============================================================
Formatted string
We use to Dynamically generate some text with variable
Example :
We want to print : John [Smith] is a coder
We can do in 2 ways :
Way 1
first = ‘John’
last = ‘Smith’
message = first + ‘ [‘ + last + ’] ’ + is a coder’
print(message)
it will print : John [Smith] is a coder
*but this way is complicated*
Examples method
course = ‘Python for Beginners’
print(course.upper()) print Uppercase
print(course.lower()) print Lowercase
print(course.find(‘P’)) print index of the letter which is 0
print(course.replace(‘Beginners’, ‘Expert’)) replace the keyword before ‘,’ to after ‘,’
print(‘Python’ in course) add Boolean to variable which mean is the word ‘Python’ is in variable
course. And it will print out true or false
ARITHMETIC OPERATION
print(10 + 10)
print(10 - 10)
print(10 * 10)
print(10 / 10)
print(10 ** 10) power of 2
print(10 % 10)
we use += to increase and -= to decrease
example :
x = 10
x += 3
print(x) it will print 13
OPERATOR PRECEDENCE (basic math)
Parenthesis () Exponentiation (**) Multiplication or division Addition or substraction
*this is precedence in math
MATH FUNCTION
X = 3.7
print(round(x)) it will make builtins/bundar
print(abs(-3.7)) it will make –ve numbers to +ve
*more math module in Google : python 3 math module
IF STATEMENT
Using boolean (True or False)
It will print statement of what user choose
Example :
hot = True
if hot:
print(“it’s hot day”)
print(“Drink more water)
else:
print(“it’s cold day”)
print(“wear warm cloth”)
LOGICAL OPERATOR
If statement is use for one condition. When in multiple condition, we use Logical Operator
We use AND, OR and NOT logical operator
Example : we want to make program : if applicant has high income AND good credit Eligible for
loan
is_high_income = True
is_good_credit = True
//(AND)
if is_high_income and is_good_credit :
print(“Eligible for loan”)
//(OR)
if is_high_income or is_good_credit :
print(“Eligible for loan”)
COMPARISON OPERATOR
To compare VARIABLES with VALUE
We use >, <, =>, <=, and == symbols for comparison
Examples :
We want to type program for user to put their name :
If name less than 3 charecters long
We print : name must be at least 3 charecters long
If name more than 20 charecters long
We print : name can be maximum of 20 charectors
if len(name) <3:
print(‘name must be at least 3 charecters long’)
if len(name) >20:
print(name can be maximum of 20 charectors’)
if input.upper() == “L”:
converted = weight * 0.45
print(f ‘You are {converted} kilos’)
if input.upper() == “K”:
converted = weight / 0.45
print(f ‘You are {converted} pounds’)
if unit.upper() == 'M':
converted1 = height * 100
converted2 = height * 1500
converted3 = height / 0.254
print(f'You are {converted1} cm {converted2} mm {converted3} inch')
elif unit.upper() == 'CM':
converted4 = height / 100
converted5 = height * 10
converted6 = height / 2.54
print(f'You are {converted4} m {converted5} mm {converted} inch ')
elif unit.upper() == 'MM':
converted7 = height / 1000
converted8 = height / 10
converted9 = height / 25.4
print(f'You are {converted7} m {converted8} cm {converted} inch')
elif unit.upper() == 'INCH':
converted10 = height / 39.4
converted11 = height * 2.54
converted12 = height * 25.4