In [6]:
s1 = "welcome"
In [7]:
s2 = "welcome"
In [8]:
id(s1)
Out[8]:
1655897342832
In [9]:
id(s2)
Out[9]:
1655897342832
In [10]:
s3 = "Welcome"
In [11]:
id(s3)
Out[11]:
1655897563184
In [12]:
s3 = "welcome"
In [13]:
id(s3)
Out[13]:
1655897342832
In [14]:
s1
Out[14]:
'welcome'
In [15]:
s1
Out[15]:
'welcome'
In [16]:
s1[0]
Out[16]:
'w'
In [17]:
len(s1)
Out[17]:
7
In [18]:
min(s1)
Out[18]:
'c'
In [20]:
s3 = "Welcome"
In [21]:
min(s3)
Out[21]:
'W'
In [22]:
s1
Out[22]:
'welcome'
In [23]:
s1[7]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [23], in <cell line: 1>()
----> 1 s1[7]

IndexError: string index out of range
In [24]:
s1[6]
Out[24]:
'e'
In [25]:
s1[0]
Out[25]:
'w'
In [26]:
s1[len(s1)-1]
Out[26]:
'e'
In [27]:
s1
Out[27]:
'welcome'
In [28]:
s4 = "Python"
In [31]:
s1 + " to " + s4
Out[31]:
'welcome to Python'
In [32]:
2 * s1
Out[32]:
'welcomewelcome'
In [34]:
3 * (s1 + " ")
Out[34]:
'welcome welcome welcome '
In [36]:
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'
In [37]:
s1
Out[37]:
'welcome'
In [38]:
s1[0:3]
Out[38]:
'wel'
In [39]:
s1[3:6]
Out[39]:
'com'
In [40]:
s1[3:7]
Out[40]:
'come'
In [42]:
s1[6]
Out[42]:
'e'
In [43]:
s1[3:]
Out[43]:
'come'
In [44]:
s1[:4]
Out[44]:
'welc'
In [45]:
s1
Out[45]:
'welcome'
In [46]:
"l" in s1
Out[46]:
True
In [47]:
"k" in s1
Out[47]:
False
In [48]:
"wel" in s1
Out[48]:
True
In [49]:
s1[-1]
Out[49]:
'e'
In [52]:
s1[-4:-1]
Out[52]:
'com'
In [53]:
"k" not in s1
Out[53]:
True
In [54]:
s1
Out[54]:
'welcome'
In [56]:
for ch in s1:
    print(ch)
w
e
l
c
o
m
e
In [57]:
for i in range(0,len(s1)):
    print(s1[i])
w
e
l
c
o
m
e
In [58]:
for i in range(0,len(s1),2):
    print(s1[i])
w
l
o
e
In [61]:
"madam"
"ada"
"adam"
Out[61]:
'adam'
In [65]:
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
In [68]:
"abc".center(20)
Out[68]:
'        abc         '
In [69]:
s5 = " \t   abc  \n     "
In [71]:
s5.rstrip()
Out[71]:
' \t   abc'
In [72]:
s5.lstrip()
Out[72]:
'abc  \n     '
In [73]:
s5.strip()
Out[73]:
'abc'
In [74]:
s5.upper()
Out[74]:
' \t   ABC  \n     '
In [75]:
s1
Out[75]:
'welcome'
In [77]:
s1.find("l")
Out[77]:
2
In [78]:
s1.find("k")
Out[78]:
-1
In [79]:
s6 = "232323"
In [81]:
s6.isdigit()
Out[81]:
True
In [82]:
s7 = "A232323"
In [83]:
s7.isdigit()
Out[83]:
False
In [84]:
0b101
Out[84]:
5
In [85]:
1 * (2**2) + 0 * (2**1) + 1 * (2**0)
Out[85]:
5
In [86]:
0xA1
Out[86]:
161
In [90]:
10 * (16**1) + 1 * (16 ** 0)
Out[90]:
161
In [91]:
0x11
Out[91]:
17
In [92]:
1 * (16 ** 1) + 1 * (16 ** 0)
Out[92]:
17
In [ ]:
 
In [96]:
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
In [95]:
hexCharToDecimal("F")
Out[95]:
15
In [97]:
0b1101101
Out[97]:
109
In [98]:
b = "1101101"
In [101]:
g1 = b[len(b)-3:len(b)]
In [102]:
g1
Out[102]:
'101'
In [104]:
g2 = b[len(b)-6:len(b)-3]
In [105]:
g2
Out[105]:
'101'
In [107]:
0o155
Out[107]:
109
In [108]:
5 + 6
Out[108]:
11
In [109]:
"ab" + "gg"
Out[109]:
'abgg'
In [ ]:
r1 + r2
In [ ]:
1 / 3
In [110]:
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
In [112]:
# 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
In [113]:
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()
In [114]:
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()
In [115]:
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()
In [ ]:
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()
In [ ]: