2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
Python Basic by Mrittika Megaraj
Introduction to Python
Code : To write codes
Markdown : To write formatted text
Raw NBConvert : To write unformatted text
Jupyter Notebook : One editor where you can write codes, formatted text,
unformatted text, create charts and graphs etc
Esc + DD : To delet the cell
Run the cell : shift + enter
In [1]: print('Hello Everyone')
Hello Everyone
In [2]: print("Hi Today I am learning Python")
Hi Today I am learning Python
Variables
In Python, variables are used to store values that can be referenced and
manipulated within a program. Variables in Python are dynamically typed,
which means you don't need to explicitly declare their types. Here's an
explanation of variables and the commonly used data types in Python:
Variable Naming: To create a variable, you need to choose a name that
follows certain rules. Variable names can contain letters (a-z, A-Z),
digits (0-9), and underscores (_), but they cannot start with a digit.
Additionally, Python is case-sensitive, so myVar and myvar would be
considered different variables.
Assigning Values: To assign a value to a variable, use the assignment
operator =. For example:
my_var = 10
Datatypes
Str : Any combination of alphabets, numbers and special characters
int : whole numbers both positive as well as negative including 0
float : decimal numbers
Bool : True /False
type() function : To check the datatype
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 1/9
2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
variables : containers that hold some value
In [3]: type("Hello")
Out[3]: str
In [4]: type(123)
Out[4]: int
In [5]: type(12.34)
Out[5]: float
In [6]: type(True)
Out[6]: bool
In [7]: type("123.45")
Out[7]: str
In [8]: type("true")
Out[8]: str
In [9]: x=10
In [10]: x
Out[10]: 10
In [11]: type(x)
Out[11]: int
In [12]: student_name="John"
In [13]: type(student_name)
Out[13]: str
In [14]: print("Student name is ",student_name)
Student name is John
Operators
Arithmatic operators : +,-,/,*,%(modulus),**
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 2/9
2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
Relational operators/Comparison operator : >,>=,<,<=,==,!=
Assignment operator : =
Increment/Decrement operator : +=,-=
In [15]: 12/5
Out[15]: 2.4
In [16]: 12%5
Out[16]: 2
In [17]: 5**3
Out[17]: 125
In [18]: 2==5
Out[18]: False
In [19]: Age=25
In [20]: Age
Out[20]: 25
In [21]: Age-=5
In [22]: Age-=1
In [23]: Age
Out[23]: 19
input () function : To accept values from users
No matter what user enter, it will be always stored as string
Casting function : int() : It will convert the string to integer
In [24]: student_name=input("Please enter your name")
print("Your name is",student_name)
Please enter your nameMrittika
Your name is Mrittika
In [25]: Age=int(input("Please enter your age"))
print("Your age is",Age)
Please enter your age21
Your age is 21
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 3/9
2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
In [26]: type(student_name)
Out[26]: str
In [27]: type(Age)
Out[27]: int
Conditionals
if else statement : Conditional
if condtion
else
nested if
elif : To check multiple conditions
In [28]: # program to check voting eligibility
age=int(input("Please enter your age"))
if age>=21:
print("You are eligible to vote")
else:
print("You are not eligible to vote")
Please enter your age21
You are eligible to vote
In [29]: age=input("Please enter your age")
if age.isdigit():
if int(age)>=21:
print("You are eligible to vote")
else:
print("You are not eligible to vote")
else :
print("Invalid input")
Please enter your age14
You are not eligible to vote
In [30]: # program to assign a grade on the basis of marks
marks=int(input("Please enter your marks"))
if marks>=90:
print("Your Grade is A")
elif marks>=80:
print("Your Grade is B")
elif marks>=70:
print("Your Grade is C")
else:
print("Your Grade is D")
Please enter your marks90
Your Grade is A
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 4/9
2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
Loops
While loop
break : To terminate the loop
for loop
In [31]: # program to print numbers from 1 to 10
x=1
while x<=10:
print(x)
x+=1
1
2
3
4
5
6
7
8
9
10
In [32]: # Program to guess a correct no
correct_no =7
x=1
while x<=3:
num=int(input("Please enter a no between 1 and 10"))
if num==correct_no:
print("Congratulations !! You have won a jackpot")
break
else:
x+=1
Please enter a no between 1 and 1010
Please enter a no between 1 and 1010
Please enter a no between 1 and 105
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 5/9
2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
In [33]: # Program to count no of vowels and consonants in the word
word=input("Please enter a word")
vowel=0
consonants=0
for i in word:
if i in ("a","e","i","o","u"):
vowel+=1
else:
consonants+=1
print("No of vowels are",vowel)
print("No of consonants are",consonants)
Please enter a wordMrittika
No of vowels are 3
No of consonants are 5
String Slicing
Positive Index : 0
Negative Index : -1
String slicing : string[start:Stop:Step]
it stop at stop -1 index
default start index : 0
default stop index : last index
default step : 1
In [34]: word="Acknowledgement"
In [35]: word[1]
Out[35]: 'c'
In [36]: word[-2]
Out[36]: 'n'
In [37]: word[0:4]
Out[37]: 'Ackn'
In [38]: word[3:8]
Out[38]: 'nowle'
In [39]: word[:4]
Out[39]: 'Ackn'
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 6/9
2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
In [40]: word[3:]
Out[40]: 'nowledgement'
In [41]: word[:]
Out[41]: 'Acknowledgement'
In [42]: word[::2]
Out[42]: 'Akoldeet'
In [43]: word[::-1]
Out[43]: 'tnemegdelwonkcA'
Importing a file
In [44]: !curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/maste
% Total % Received % Xferd Average Speed Time Time Time C
urrent
Dload Upload Total Spent Left S
peed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:--
0
100 56 100 56 0 0 125 0 --:--:-- --:--:-- --:--:--
126
Opening a Local File in read mode
poem_file = open('poem1.txt', 'r')
Read mode 'r'
MODE and Description
'r'-read only mode
'w'-write - overwrites file with same name
'r+'-read and write mode
'a'-opens for appending to end of file
open() creates an object that can be addressed in python code
In [45]: poem=open('poem1.txt','r')
In [46]: poem_read=poem.readlines()
In [47]: type(poem_read)
Out[47]: list
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 7/9
2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
In [48]: print(poem)
<_io.TextIOWrapper name='poem1.txt' mode='r' encoding='cp1252'>
In [49]: print(poem_read)
['Loops I repeat\n', 'loops\n', 'loops\n', 'loops\n', 'I repeat\n', 'unti
l I\n', 'break\n']
In [50]: poem.close()
In [51]: for i in poem_read:
print(i)
Loops I repeat
loops
loops
loops
I repeat
until I
break
In [52]: # [ ] define and call a function short_rhyme() that prints a 2 line rhyme
def short_rhyme():
print("Roses are red,")
print("Violets are blue.")
# Call the function
short_rhyme()
Roses are red,
Violets are blue.
# [ ] define (def) a simple function: title_it() and call the function
# - has a string parameter: msg
# - prints msg in Title Case
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 8/9
2/2/24, 8:54 PM Python Basic_Day1 - Jupyter Notebook
In [54]: def title_it(msg):
print(msg.title())
# Call the function
message = "hello, world!"
title_it(message)
Hello, World!
# [ ] get user input with prompt "what is the title?"
# [ ] call title_it() using input for the string argument
In [55]: def title_it(msg):
print(msg.title())
# Get user input
user_input = input("What is the title? ")
# Call the function using user input
title_it(user_input)
What is the title? sherlock holmes
Sherlock Holmes
# [ ] define title_it_rtn() which returns a titled string instead of
printing
# [ ] call title_it_rtn() using input for the string argument and print
the result
In [56]: def title_it_rtn(msg):
return msg.title()
# Get user input
user_input = input("What is the title? ")
# Call the function and print the result
result = title_it_rtn(user_input)
print(result)
What is the title? sherlock holmes
Sherlock Holmes
In [ ]:
localhost:8888/notebooks/100 days data science challenge/Python Basic_Day1.ipynb 9/9