Class 01 Notes
Class 01 Notes
Let's have a look at Python as a language and what features does it bring to be among the top 3 coding
languages in the world.
# 1. Unlike other languages Python program works on an Interpreter so to run a code all you have to do is
type it out
print("Welcome to NUCOT")
# 2. Python language runs line by line, so incase you want to update a piece of code all you have to do is
update the line let's see how -
#Imaginary giraffe
print("O ")
print("| ")
print("|____")
print(" ||||")
print(" ||||")
Now let's see what happened here - Any character whether it be numeric or text enclosed within " " or ' '
gets considered as a string; A string is a data type which contains a series of characters. Just like string,
there are numerous types of variables in Python; let's have a look
Python variables are a container that can store any type of value and of any particular size.
Unlike other languages such as C, Java - Variables in Python don't need initialization. Let's create a
variable to see how do they work
var1 = "Mike"
print(var1)
var2 = 6
print(var2)
Now that we know how to store a variable in Python let's try getting input from a user and storing it in
Python
#To get a input from a user we would use the 'input' function
number = input("What's your favourite number? ")
print(number)
age = int(age)
Here in the below piece of code you can see that we have assigned each variable with a particular value
and use the type method we can check the datatype of the variable
integer_num = 1
floating_num = 1.3
string = 'Mike'
boolean = True
print (type(integer_num))
print (type(floating_num))
print (type(string))
print (type(boolean))
Arithmatic Operations
a=6
b=7
print("Addition = ", a + b)
print("Substraction = ", a - b)
print("Multiplication = ", a * b)
print("Division = ", a / b)
print("Floor Division = ", a // b)
print("Modulus or remainder",a % b)
print("Exponential = ", a ** b)
Now if you recall that the mathematical operators follow a precedence rule called PEDMAS -
Wherein the fist P is parenthesis, then it's E for exponents, M for multiplication, then D for division, A for
addition and finally S for subtraction.
print((4 * 5) - 9 + 6 / 7)
String Operations
We should look at strings as a banner where multiple characters are linked together. The string data is an
immutable type which means it cannot be modified.
#Whenever we are assigning a char or string we use '' which represents a character
c = 'z'
d = 'Mike'
#You can also use " " double quotes instead of single quotes for strings
e = "John"
#Now when we want to include words with a single quote such as "didn't", "I'll".. we might end up with a
error
print('I didn't know that')
#Now that '\' escape character which we saw earlier can also be used for other stuff like adding a new line
print("Hi Bob \n\nHow are you?")
Now apart adding new lines to a string we can also add another to a pre-existing string, let's see how -
###String Concatenation
#Let's try building a system that could ask for flavour, and the type of desert user wants
flavour=input("What flavour would you like ")
dessert_type=input("What type of dessert would you like ")
print("You have ordered", flavour+"-"+dessert_type)
##String Manipulation
- Strings are immutable which, means they cannot be changed once created.
# Len() function
string = "I have not failed. I’ve just found 10,000 ways that won’t work. – Thomas A. Edison"
len(string)
- string[start:end:step]
print(batch[ 8: ])
# Membership
String = "John! Did you attend the conference on advanced machine learning"
print( 'Lincoln' in String)
print ( 'Behzad' not in String)
- upper()
- lower()
- strip()
- count(substring,begin,end)
- https://docs.python.org/3.7/library/stdtypes.html#string-methods
# upper()
small = "i am upper cased"
print(small.upper())
# lower()
large = "I AM LOWER CASED"
print(large.lower())
# rstrip () function
some_sentence = "There is a space at the end "
print(some_sentence)
print(some_sentence.rstrip())
increment = '4%'
print(increment.rstrip('%'))
# lstrip() function
start = " There is space at the start"
print(start)
print(start.lstrip())
# strip() function
spaces = " Trim whitespaces "
print(spaces)
print(spaces.strip())
num_with_chars = '******444#######'
print(num_with_chars.rstrip('#').lstrip('*'))
#count() Method
string = "This is a sample sentence"
print(string.count('i'))
print(string.count('i',5))
#r/R It is used to specify the raw string.% It is used to perform string formatting.
A = "Data"
B = "Analysis"
C = "Pandas"
#Now there are a plenty of other string operations available in Python you could explore those using
help(str)