ML Lab 01 - Introduction To Python
ML Lab 01 - Introduction To Python
Introduction to Python
MUNADI SIAL
• Let’s write a simple python script and execute it via the terminal:
a = 10
b = “M”
c = 7.6
print(a,b,c)
• Once you have typed the above code, save the script as test.py
• Now open the terminal, go the directory (with cd) where you saved the script
and execute it with the following command:
python test.py
Variables in Python
• To add a comment in code, use # in Python
• The data type of a variable can change in Python:
a = 10 # this will work
a = 5.5 # this will work
a = “house” # this will work
add = a + b
sub = a - b
mul = a*b
div = a/b
quotient = a//b
remainder = a%b
power = a**b
print(not a) print(not b)
>> False >> True
Strings
• Python supports the string data type which is an array of characters
h = “Manipulator”
print(h) Output: Manipulator
• You can get the number of characters with the len() function
g = len(h)
print(g) Output: 11
Strings
• You can concatenate strings easily in python
c = “Computer”
d = “Vision” Output: ComputerVision
e = c + d
print(e)
f = c + “ “ + d
print(f) Output: Computer Vision
• You can check if a character is present in the string with the “in” keyword
• To compute the numeric sum, we need to convert the string data type to an int or
float data type
v1 = int(v1)
v2 = int(v2) Output: 300
print(v1 + v2)
If…Else
• Python supports the IF…ELSE conditional statements which choose to execute
statements depending if a condition is true or false
• The syntax for a IF statement is given as:
if <condition>:
<statement_1>
<statement_2>
• An example is given below:
a = 4
b = 3
if a > b:
print(“a is greater than b”)
Output: 3 + 4 = 7
value = 7
Lab Tasks