s1 = "welcome"
s2 = "welcome"
id(s1)
1655897342832
id(s2)
1655897342832
s3 = "Welcome"
id(s3)
1655897563184
s3 = "welcome"
id(s3)
1655897342832
s1
'welcome'
s1
'welcome'
s1[0]
'w'
len(s1)
7
min(s1)
'c'
s3 = "Welcome"
min(s3)
'W'
s1
'welcome'
s1[7]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Input In [23], in <cell line: 1>() ----> 1 s1[7] IndexError: string index out of range
s1[6]
'e'
s1[0]
'w'
s1[len(s1)-1]
'e'
s1
'welcome'
s4 = "Python"
s1 + " to " + s4
'welcome to Python'
2 * s1
'welcomewelcome'
3 * (s1 + " ")
'welcome welcome welcome '
3 + s1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [36], in <cell line: 1>() ----> 1 3 + s1 TypeError: unsupported operand type(s) for +: 'int' and 'str'
s1
'welcome'
s1[0:3]
'wel'
s1[3:6]
'com'
s1[3:7]
'come'
s1[6]
'e'
s1[3:]
'come'
s1[:4]
'welc'
s1
'welcome'
"l" in s1
True
"k" in s1
False
"wel" in s1
True
s1[-1]
'e'
s1[-4:-1]
'com'
"k" not in s1
True
s1
'welcome'
for ch in s1:
print(ch)
w e l c o m e
for i in range(0,len(s1)):
print(s1[i])
w e l c o m e
for i in range(0,len(s1),2):
print(s1[i])
w l o e
"madam"
"ada"
"adam"
'adam'
def main():
# Prompt the user to enter a string
s = input("Enter a string: ").strip()
if isPalindrome(s):
print(s, "is a palindrome")
else:
print(s, " is not a palindrome")
# Check if a string is a palindrome
def isPalindrome(s):
# The index of the first character in the string
low = 0
# The index of the last character in the string
high = len(s) - 1
while low < high:
if s[low] != s[high]:
return False # Not a palindrome
low += 1
high -= 1
return True # The string is a palindrome
main() # Call the main function
Enter a string: madam madam is a palindrome
"abc".center(20)
' abc '
s5 = " \t abc \n "
s5.rstrip()
' \t abc'
s5.lstrip()
'abc \n '
s5.strip()
'abc'
s5.upper()
' \t ABC \n '
s1
'welcome'
s1.find("l")
2
s1.find("k")
-1
s6 = "232323"
s6.isdigit()
True
s7 = "A232323"
s7.isdigit()
False
0b101
5
1 * (2**2) + 0 * (2**1) + 1 * (2**0)
5
0xA1
161
10 * (16**1) + 1 * (16 ** 0)
161
0x11
17
1 * (16 ** 1) + 1 * (16 ** 0)
17
def main():
# Prompt the user to enter a hex number
hex = input("Enter a hex number: ").strip()
decimal = hexToDecimal(hex.upper())
if decimal == None:
print("Incorrect hex number")
else:
print("The decimal value for hex number",
hex, "is", decimal)
def hexToDecimal(hex):
decimalValue = 0
for i in range(len(hex)):
ch = hex[i]
if 'A' <= ch <= 'F' or '0' <= ch <= '9':
decimalValue = decimalValue * 16 + \
hexCharToDecimal(ch)
else:
return None
return decimalValue
def hexCharToDecimal(ch):
if 'A' <= ch <= 'F':
return 10 + ord(ch) - ord('A')
else:
return ord(ch) - ord('0')
main() # Call the main function
Enter a hex number: 11 The decimal value for hex number 11 is 17
hexCharToDecimal("F")
15
0b1101101
109
b = "1101101"
g1 = b[len(b)-3:len(b)]
g1
'101'
g2 = b[len(b)-6:len(b)-3]
g2
'101'
0o155
109
5 + 6
11
"ab" + "gg"
'abgg'
r1 + r2
1 / 3
class Rational:
def __init__(self, numerator = 0, denominator = 1):
divisor = gcd(numerator, denominator)
self.__numerator = (1 if denominator > 0 else -1) \
* int(numerator / divisor)
self.__denominator = int(abs(denominator) / divisor)
# Add a rational number to this rational number
def __add__(self, secondRational):
n = self.__numerator * secondRational[1] + \
self.__denominator * secondRational[0]
d = self.__denominator * secondRational[1]
return Rational(n, d)
# Subtract a rational number from this rational number
def __sub__(self, secondRational):
n = self.__numerator * secondRational[1] - \
self.__denominator * secondRational[0]
d = self.__denominator * secondRational[1]
return Rational(n, d)
# Multiply a rational number to this rational
def __mul__(self, secondRational):
n = self.__numerator * secondRational[0]
d = self.__denominator * secondRational[1]
return Rational(n, d)
# Divide a rational number by this rational number
def __truediv__(self, secondRational):
n = self.__numerator * secondRational[1]
d = self.__denominator * secondRational[0]
return Rational(n, d)
# Return a float for the rational number
def __float__(self):
return self.__numerator / self.__denominator
# Return an integer for the rational number
def __int__(self):
return int(self.__float__())
# Return a string representation
def __str__(self):
if self.__denominator == 1:
return str(self.__numerator)
else:
return str(self.__numerator) + "/" + str(self.__denominator)
def __lt__(self, secondRational):
return self.__cmp__(secondRational) < 0
def __le__(self, secondRational):
return self.__cmp__(secondRational) <= 0
def __gt__(self, secondRational):
return self.__cmp__(secondRational) > 0
def __ge__(self, secondRational):
return self.__cmp__(secondRational) >= 0
# Compare two numbers
def __cmp__(self, secondRational):
temp = self.__sub__(secondRational)
if temp[0] > 0:
return 1
elif temp[0] < 0:
return -1
else:
return 0
# Return numerator and denominator using an index operator
def __getitem__(self, index):
if index == 0:
return self.__numerator
else:
return self.__denominator
def gcd(n, d):
n1 = abs(n);
n2 = abs(d)
gcd = 1
k = 1
while k <= n1 and k <= n2:
if n1 % k == 0 and n2 % k == 0:
gcd = k
k += 1
return gcd
# Create and initialize two rational numbers r1 and r2.
r1 = Rational(4, 2)
r2 = Rational(2, 3)
# Display results
print(r1, "+", r2, "=", r1 + r2)
print(r1, "-", r2, "=", r1 - r2)
print(r1, "*", r2, "=", r1 * r2)
print(r1, "/", r2, "=", r1 / r2)
print(r1, ">", r2, "is", r1 > r2)
print(r1, ">=", r2, "is", r1 >= r2)
print(r1, "<", r2, "is", r1 < r2)
print(r1, "<=", r2, "is", r1 <= r2)
print(r1, "==", r2, "is", r1 == r2)
print(r1, "!=", r2, "is", r1 != r2)
print("int(r2) is", int(r2))
print("float(r2) is", float(r2))
print("r2[0] is", r2[0])
print("r2[1] is", r2[1])
2 + 2/3 = 8/3 2 - 2/3 = 4/3 2 * 2/3 = 4/3 2 / 2/3 = 3 2 > 2/3 is True 2 >= 2/3 is True 2 < 2/3 is False 2 <= 2/3 is False 2 == 2/3 is False 2 != 2/3 is True int(r2) is 0 float(r2) is 0.6666666666666666 r2[0] is 2 r2[1] is 3
import tkinter
from tkinter import messagebox
#callback function
def do_something():
#the following line of code show messagebox
messagebox.showinfo('Response', 'You have clicked the button')
def main():
# Create a root window
root = tkinter.Tk()
# create a button widget
button = tkinter.Button(root, text="Click Me", command = do_something)
button.pack()
# Call the event loop
root.mainloop()
# Call the function main
main()
import tkinter
window = tkinter.Tk()
window.title("GUI")
# creating 2 frames TOP and BOTTOM
top_frame = tkinter.Frame(window).pack()
bottom_frame = tkinter.Frame(window).pack(side = "bottom")
# now, create some widgets in the top_frame and bottom_frame
btn1 = tkinter.Button(top_frame, text = "Button1", fg = "red").pack()# 'fg - foreground' is used to color the contents
btn2 = tkinter.Button(top_frame, text = "Button2", fg = "green").pack()# 'text' is used to write the text on the Button
btn3 = tkinter.Button(bottom_frame, text = "Button3", fg = "purple").pack(side = "left")# 'side' is used to align the widgets
btn4 = tkinter.Button(bottom_frame, text = "Button4", fg = "orange").pack(side = "left")
window.mainloop()
import tkinter as tk
root = tk.Tk()
w = tk.Label(root, text="Red Sun", bg="red", fg="white")
w.pack()
w = tk.Label(root, text="Green Grass", bg="green", fg="black")
w.pack(fill=tk.X)
w = tk.Label(root, text="Blue Sky", bg="blue", fg="white")
w.pack(fill=tk.X)
tk.mainloop()
import tkinter as tk
colours = ['red','green','orange','white','yellow','blue']
r = 0
for c in colours:
tk.Label(text=c, relief=tk.RIDGE, width=15).grid(row=r,column=0)
tk.Entry(bg=c, relief=tk.SUNKEN, width=10).grid(row=r,column=1)
r = r + 1
tk.mainloop()