100 Days of Code in Pyhton
100 Days of Code in Pyhton
100 Days of Code in Pyhton
What is Python?
Features of Python
Why Replit?
• Welcome to Day 2 of 100 days of code. Let me start with a story! Back
in my college, I used to learn C and C++ programming in depth, used
to score good marks. I created a bunch of printing, conditionals and
loop program. Now what? I wanted to benefit from the same In my
second year of college, I started working (I mean actually working in the
industry) with the python programming language. I was not so good
with it but I used to write code for a singaporean client and actually
make good money without having to actually master Python. Harry then
got curious and started working on his Python skills even more. I then
got into web scraping and trust me I made some good easy money on
Fiverr just by writing some python programs and charging on per
webpage basis to my clients ( I used to automate scraping)
• I then learnt flask and got to work with Flask with a university
professor abroad. Long story short, Python made a huge impact in my
career.
• I want to show you some python programs I created which will surely
inspire you to create your own versions of the same as we progress
through this tutorial. Do not try to recreate them just yet if you are a
beginner and just started working on Python. We will make progress
gradually trust me.
1. Built in Modules - These modules are ready to import and use and
ships with the python interpreter. there is no need to install such
modules explicitly.
2. External Modules - These modules are imported from a third-party
file or can be installed using a package manager like pip or conda.
Since this code is written by someone else, we can install different
versions of a same module with time.
import pandas
Similarly, we can install other modules and look into their documentations
for usage, instructions. We will find ourselves doing this often in the later part
of this course
Quick Quiz
Write a program to print a poem in Python. Choose the poem of your choice
and publish your repl
Python Comments
A comment is a part of the coding file that the programmer does not want to
execute, rather the programmer uses it to either explain a block of code or to
avoid the execution of a specific part of code while testing.
Single-Line Comments:
Example 1
Example 2
print("Hello World !!!") #Printing Hello World
Output: Hello World !!!
Example 3:
print("Python Program")
# print("Python Program")
Output: Python Program
Multi-Line Comments:
To write multi-line comments you can use ‘#’ at each line or you can use the
multiline string.
Variable is like a container that holds data. Very similar to how our containers
in kitchen holds sugar, salt etc Creating a variable is like creating a
placeholder in memory and assigning it some value. In Python its as easy as
writing:
a=1
b = True
c = "Harry"
d = None
Data type specifies the type of value a variable holds. This is required in
programming to do various operations without causing an error.
In python, we can print the type of any operator using type function:
a=1
print(type(a))
b = "1"
print(type(b))
int: 3, -8, 0
float: 7.349, -9.0, 0.0000001
complex: 6 + 2i
3. Boolean data:
Example:
Example:
Example:
Arithmetic operators
Exercise
n = 15
m=7
ans1 = n+m
print("Addition of",n,"and",m,"is", ans1)
ans2 = n-m
print("Subtraction of",n,"and",m,"is", ans2)
ans3 = n*m
print("Multiplication of",n,"and",m,"is", ans3)
ans4 = n/m
print("Division of",n,"and",m,"is", ans4)
ans5 = n%m
print("Modulus of",n,"and",m,"is", ans5)
ans6 = n//m
print("Floor Division of",n,"and",m,"is", ans6)
Explanation
Here 'n' and 'm' are two variables in which the integer value is being stored.
Variables 'ans1', 'ans2', 'ans3', 'ans4','ans5' and 'ans6' contains the outputs
corresponding to addition, subtraction, multiplication, division, modulus, and
floor division respectively.
Day 7: Exercise Code and Output
The conversion of one data type into the other data type is known as type
casting in python or type conversion in python.
Python supports a wide variety of functions or methods like: int(), float(), str(),
ord(), hex(), oct(), tuple(), set(), list(), dict(), etc. for the type casting in python.
The conversion of one data type into another data type, done via developer or
programmer's intervention or manually as per the requirement, is known as
explicit type conversion.
It can be achieved with the help of Python’s built-in type conversion functions
such as int(), float(), hex(), oct(), str(), etc .
string = "15"
number = 7
string_number = int(string) #throws an error if the string is not a valid integer
sum= number + string_number
print("The Sum of both the numbers is: ", sum)
Output:
Data types in Python do not have the same level i.e. ordering of data types is
not the same in Python. Some of the data types have higher-order, and some
have lower order. While performing any operations on variables with different
data types in Python, one of the variable's data types will be changed to the
higher data type. According to the level, one data type is converted into other
by the Python interpreter itself (automatically). This is called, implicit
typecasting in python.
Python converts a smaller data type to a higher data type to prevent data loss.
Ouput:
<class 'int'>
<class 'float'>
10.0
<class 'float'>
Syntax:
variable=input()
But input function returns the value as string. Hence we have to typecast
them whenever required to another datatype.
Example:
variable=int(input())
variable=float(input())
We can also display a text using input function. This will make input()
function take user input and display a message as well
Example:
Example
name = "Harry"
print("Hello, " + name)
Output
Hello, Harry
Note: It does not matter whether you enclose your strings in single or double
quotes, the output remains the same.
Sometimes, the user might need to put quotation marks in between the
strings. Example, consider the sentence: He said, “I want to eat an apple”.
How will you print this statement in python? He said, "I want to eat an apple". We
will definitely use single quotes for our convenience
Multiline Strings
If our string has multiple lines, we can create them like this:
print(name[0])
print(name[1])
Above code prints all the characters in the string name one by one!
Length of a String
Example:
fruit = "Mango"
len1 = len(fruit)
print("Mango is a", len1, "letter word.")
Output:
String as an array
Example:
pie = "ApplePie"
print(pie[:5])
print(pie[6]) #returns character at specified index
Output:
Apple
I
Note: This method of specifying the start and end index to specify a part of a
string is called slicing.
Slicing Example:
pie = "ApplePie"
print(pie[:5]) #Slicing from Start
print(pie[5:]) #Slicing till End
print(pie[2:6]) #Slicing in between
print(pie[-8:]) #Slicing using negative index
Output:
Apple
Pie
pleP
ApplePie
Strings are arrays and arrays are iterable. Thus we can loop through
strings.
Example:
alphabets = "ABCDE"
for i in alphabets:
print(i)
Output:
A
B
C
D
E