Unit0 Intropython
Unit0 Intropython
• You should convert the inputs to int type if you want to use
them as integers, otherwise, they are treated as strings.
E.g. adding 2 strings concatenates them not add them
Task 2
• Math operations or Expression
• What formula I need?
• Example:
• Change_price = current_price - last_months_price
• Monthly_Mortgage=(current_price * 0.051) / 12
• Task 3 - Outputs
• What are the outputs?
• Listing the price,
• The change since last month
• The estimated monthly mortgage
≠ != x != y x is not equal to y
• Syntax: if test_expression:
statement(s)
• The program evaluates the test_expression and will
execute statement(s) only if the test_expression is
True
• If the test_expression is False, the statement(s) are
not executed
• An “test_expression” is a logical condition. It’s value is
True or False
• The body of the if block is indicated by indentation and
is called a suite
Syntax: if experssion1:
Example: if num == 1:
#statements
print(" first year ")
elif expression2:
elif num == 2:
#statements
print("second year")
else:
else:
#statements
print("third year")
< <= > >= == != Left to right Relational, (in)quality and membership operator
or Logical or
• Example:
In [1]: product = 3
In [2]: while product <= 50:
product = product * 3
In [3]: print (product)
Out[3]: 81
• Problem:
• Example:
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri’ ]
Weekends = [‘sat’, ‘sun’]
for weekday in weekdays:
print(weekday)
Example:
num_rows = int(input())
num_cols = int(input())
• Built-in functions:
• int, float, print, input, type, sum, len, min and max
• statistics module (mean, median and mode, Each performed
a single, well-defined task.
• User defined functions: user can define function
using def as follows
def square(number): #defining function
#Calculate the square of number
return number ** 2
square(7)