Day 05 : Comments, Escape Sequences & Print Statement
# 1. Comments : # this is for single line comment""" this is for multiple line comment"""# 2. Escape Sequences : print("This is newline \nfollow this.")
print("My favorite book is \"Bhagavad Gita.\"")
# 3. Print Statement : print("Hello Python",6,5,sep=" ~ ",end="\n")
# list: A list is an ordered collection of data with elements separated by a comma and enclosed within square brackets.Lists are mutable and can be modified after creation.list= [1,2,3,[-5,7],["Hello World"], 2.5 ]
print(list) # [1, 2, 3, [-5, 7], ['Hello World'], 2.5]print(type(list)) # <class 'list'>
# Tuple: A tuple is an ordered collection of data with elements separated by a comma and enclosed within parentheses. Tuples are immutable and can not be modified after creation.tuple= ("banana"), ("cherry"), ("mango")
print(tuple) # ('banana', 'cherry', 'mango')print(type(tuple)) # <class 'tuple'>
# Mapped data: dict# dict: A dictionary is an unordered collection of data containing a key:value pair. The key:value pairs are enclosed within curly brackets.dictionary= {"Car": "BMW", "Year": 2023}
print(dictionary) # {'Car': 'BMW', 'Year': 2023}print(type(dictionary)) # <class 'dict'>
Exercise 01 : Create a calculator capable of performing addition, subtraction, multiplication and division operations on two numbers.Your program should format the output in a readable manner!
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.
a="1"# stringb="2"# stringprint(a+b) # string + string = string# Two Types of Typecasting:#1.Explicit Conversion (Explicit type casting in python)a="1"b="2"print(int(a) +int(b)) # convert string to integer#2.Implicit Conversion (Implicit type casting in python).a=1.5# float numberb=2# int numberprint(a+b) # a + b = 3.5
Python converts a smaller data type to a higher data type to prevent data loss.
Day 10 : Taking User Input in Python
# variable = input()n=input("Enter a value : ")
print(n)
a=input("Enter a value : ")
b=input("Enter a value : ")
print(a+b) # a = 1; b = 2; output = 12
But input function returns the value as string. Hence we have to typecast them whenever required to another datatype.
a=input("Enter a value : ")
b=input("Enter a value : ")
#print(int(a) + int(b)) ---->ValueError: invalid literal for int() with base 10print(int(a) +float(b)) # a = 1; b = 2; output = 3.0
variable = data_type(input())
a=int(input("Enter an integer value: "))
b=int(input("Enter an integer value: "))
print(a+b) # a = 1; b = 2; output = 3
Day 11 : String in python
Use double quotation
text="He said, \"I am a happy person.\" "print(text)
use single quotation
text='He said, "I am a happy person." 'print(text)
multiple line string (use """ """)
text="""Hey,how are you?I am fine and You?I am fine also."""print(text)
multiple line string (use ''' ''')
text='''Hey,how are you?I am fine and You?I am fine also.'''print(text)
Accessing character of a string
str="Harry"print(str) # Calculate string lengthprint(len(str)) # length = 4print(str[0]) # Hprint(str[1]) # aprint(str[2]) # rprint(str[3]) # rprint(str[4]) # y#print(str[5]) # its not working.because of its length
str1="Bangladesh"print(str1.upper()) # BANGLADESHstr2="BANGLADESH"print(str2.lower()) # bangladeshstr3=" Bangladesh is my country. "print(str3.rstrip(" ")) # The strip() method removes any white spaces before and after the string.str4="Bangladesh is my country."print(str4.split(" ")) # create a liststr5="Bangladesh is my country."print(str5.replace("my","our")) # Bangladesh is our country.str6="bangladesh is mY Country."print(str6.capitalize()) # only 1st word capital ,otherwise lower case # output : Bangladesh is my country.str7="Bangladesh is mY Country."print(len(str7)) # 25print(len(str7.center(50))) # 50print(str7.center(50)) # Bangladesh is mY Country.str8="Bangladesh"print(str8.count("a")) # 2str9="Welcome to the Console !!!"print(str9.endswith("!!!")) # Truestr10="Welcome to the Console !!!"print(str10.endswith("to", 4, 10)) # True str11="He's name is Dan. He is an honest man."print(str11.find("ishh")) # -1# print(str1.index("ishh"))str12="WelcomeToTheConsole"print(str12.isalnum()) # Truestr13="Welcome"print(str13.isalpha()) # Truestr14="hello world"print(str14.islower()) # Truestr15="We wish you a Merry Christmas\n"print(str15.isprintable()) # Falsestr16=" "#using Spacebarprint(str16.isspace()) # Truestr17=" "#using Tabprint(str17.isspace()) # Truestr18="World Health Organization"print(str18.istitle()) # Truestr19="To kill a Mocking bird"print(str19.istitle()) # Falsestr20="Python is a Interpreted Language"print(str18.startswith("Python")) # Falsestr21="Python is a Interpreted Language"print(str21.swapcase()) # pYTHON IS A iNTERPRETED lANGUAGEstr22="His name is Dan. Dan is an honest man."print(str22.title()) # His Name Is Dan. Dan Is An Honest Man.