1-String

Sun 29 June 2025
a, b = "apple", "banana"
print(a + b)
applebanana
print(a * 2)
appleapple
print(len(a))
5
print(a[0])
a
print(a[-1])
e
print(a[1:4])
ppl
print(a[:3])
app
print(a[2:])
ple
print(a.upper())
APPLE
print(a.lower())
apple
print(a.capitalize())
Apple
print("HELLO".casefold())
hello
print(a.swapcase()) 
APPLE
print("Hello World".title()) 
Hello World
print("   spaced   ".strip())
spaced
print("   spaced   ".lstrip())
spaced
print("   spaced   ".rstrip())
   spaced
print("east-west".split("-"))
['east', 'west']
print("one two three".split())
['one', 'two', 'three']
print("a,b,c".split(","))
['a', 'b', 'c']
print(",".join(["a","b","c"]))
a,b,c
print("apple".startswith("app"))
True
print("apple".endswith("ple"))
True
print("apple".find("p")) 
1
print("apple".find("x"))
-1
print("apple".index("p")) 
1
print("apple".count("p"))
2
print("apple".replace("p", "b"))
abble
print(" a | b | c ".split("|"))
[' a ', ' b ', ' c ']
print("abc".isalpha()) 
True
print("abc123".isalnum())
True
print("123".isdigit())
True
print("   ".isspace())
True
print("Hello".islower()) 
False
print("hello".islower())
True
print("WORLD".isupper())
True
print("Hello".istitle())
True
print("Hello".encode())
b'Hello'
print(str(123)) 
123
print(repr("123\n"))
'123\n'
print(a.endswith(("le","na")))
True
print("abcDEF".upper().lower())
abcdef
print("a b c".replace(" ", "")) 
abc
print("12345".zfill(8))
00012345
print("x".rjust(3, "-"))
--x
print("x".ljust(3, "+"))
x++
print("hi".center(6, "*"))
**hi**
print("aBc".center(5))
 aBc
print("ABCD".islower())
False
print("abcd".islower())
True
print("ABCD".isupper()) 
True
print("abc123".islower()) 
True
print("abc123".isalpha())
False
print("abc".isdigit())
False
print("123abc".isalnum())
True
print("ß".casefold())
ss
print("Straße".casefold())
strasse
print("  Hello  ".strip().upper()) 
HELLO
print("-".join("ABC")) 
A-B-C
print("A-B-C".split("-"))
['A', 'B', 'C']
print("  mixedCase  ".swapcase())
  MIXEDcASE
print("python3.9".partition("."))
('python3', '.', '9')
print("nosplit".partition("."))
('nosplit', '', '')
print("a b c".rpartition(" "))
('a b', ' ', 'c')
print("abc\nxyz".splitlines())
['abc', 'xyz']
print("abc\nxyz\n".splitlines(True))
['abc\n', 'xyz\n']
print("for\tj".expandtabs(4))
for j
print("teSt".capitalize())
Test
print("🙂😊🙃".encode('utf-8'))
b'\xf0\x9f\x99\x82\xf0\x9f\x98\x8a\xf0\x9f\x99\x83'
print(" trim ".strip("*"))
 trim
print("***trim***".strip("*"))
trim
print("abc".startswith(""))
True
print("".join([]))
print("".join(["a"]))
a
print("One".lower().startswith("o"))
True
print("One".upper().endswith("E"))
True
print("spam eggs".replace("eggs","ham"))
spam ham
print(" x ".strip().zfill(4))
000x
print(" x ".strip().zfill(4))
000x
print("²³".isnumeric())  
True
print("Ⅻ".isnumeric())
True
print("20".isdecimal())
True
print("²".isdecimal())
False
print("hello".encode('ascii'))
b'hello'
print("a b c".replace(" ", "-", 2))
a-b-c
print("a--b--c".split("--",1)) 
['a', 'b--c']
print("A|B|C".split("|", maxsplit=1))
['A', 'B|C']
print("  A  B  ".split())
['A', 'B']
print("word".ljust(6)) 
word
print("word".rjust(6)) 
  word
print("WORD".lower().upper())
WORD
print(" a,b,c ".strip().split(","))
['a', 'b', 'c']
print(";".join("123"))
1;2;3
print("123".join(["a","b","c"])) 
a123b123c
print("x".center(3))
 x
print("x".center(4,"-"))
-x--
print("Data".islower(), "Data".isupper(), "Data".istitle())
False False True
print("Data123".isalnum())
True
print("  ".replace(" ","_"))
__
print("A\nB\nC".splitlines())
['A', 'B', 'C']
print("\n".join(["A","B","C"]))
A
B
C
print("trimmeR".rstrip("Rr"))
trimme
print("trimmeR".lstrip("tr"))
immeR
print("foobar".partition("o"))
('f', 'o', 'obar')
print("foobar".rpartition("o"))
('fo', 'o', 'bar')
print(" repeat ".strip().replace(" ","-"))
repeat
print(" repeat ".strip().replace(" ", "-"))
repeat
print("aBcDeF".swapcase()[::-1]) 
fEdCbA
print("12345".translate(str.maketrans("135","ACE")))
A2C4E
print("hello😊".encode('utf-8').decode('utf-8'))
hello😊
print("test".format())
test
print("{0} + {1}".format("a","b"))
a + b
print(f"{a}-{b}") 
apple-banana
print("%s:%d" % ("age", 30)) 
age:30
print(" abc ".strip().title())
Abc
print("mix".zfill(5).upper())
00MIX
print("END".center(7, "."))
..END..
print("wrap".rjust(6,"~").ljust(8,"_"))

Score: 115

Category: basics


2-List

Sun 29 June 2025
list = [
    "AB", 
    "CD",
    "EF",
    "GH",
    "IJ", 
    "KL",
    "MN",
    "OP",
    "QR",
    "ST",
    "UV",
    "WX",
    "YZ"
]
list
['AB', 'CD', 'EF', 'GH', 'IJ', 'KL', 'MN', 'OP', 'QR', 'ST', 'UV', 'WX', 'YZ']
# append to list
list.append("ABC")
list
['AB',
 'CD',
 'EF',
 'GH',
 'IJ',
 'KL',
 'MN',
 'OP',
 'QR',
 'ST',
 'UV',
 'WX',
 'YZ',
 'ABC']
list …

Category: basics

Read More

3-Dictionary

Sun 29 June 2025
d = {"a": 1, "b": 2}
print(d)
{'a': 1, 'b': 2}
print(len(d))
2
print(d["a"])
1
d["c"] = 3
print(d)
{'a': 1, 'b': 2, 'c': 3}
d["a"] = 10
print(d)
{'a': 10, 'b': 2, 'c': 3}
del d["b"]
print(d)
{'a': 10, 'c': 3 …

Category: basics

Read More

4-Set

Sun 29 June 2025
s = {1, 2, 3}
print(s)
{1, 2, 3}
print(len(s)) 
3
print(2 in s)
True
print(2 in s)
True
s.add(4)
print(s)
{1, 2, 3, 4}
s.remove(3)
print(s)
{1, 2, 4}
s.discard(5)
print(s)
{1, 2, 4}
s.update …

Category: basics

Read More

5-Tuple

Sun 29 June 2025
#Tuple Basics
t = (1, 2, 3)
print(t)
(1, 2, 3)
print(type(t))
<class 'tuple'>
t2 = tuple([4, 5, 6])
print(t2)
(4, 5, 6)
t3 = 1, 2, 3
print(t3)
(1, 2, 3)
t4 = (1,)  # single element
print(t4)
(1,)
print(len(t))
3
print(t[0])
1 …

Category: basics

Read More

6-File

Sun 29 June 2025
#Basic File Reading/Writing
f = open("sample.txt", "w")
f.write("Hello")
f.close()
f = open("sample.txt", "r")
print(f.read())
f.close()
Hello
with open("sample.txt", "r") as f:
    print(f.read())
Hello
with open("sample.txt", "w") as f:
    f.write("New content")
with open("sample …

Category: basics

Read More

Abstract-With-Ast

Sun 29 June 2025
!python --version
Python 3.12.11
import ast
# Python code as a string
code = """
def greet(name):
    return f"Hello, {name}!"

x = 10
y = x + 20
print(greet("Python"))
"""
# Parse the code into an AST
tree = ast.parse(code)
# Define a visitor class to analyze nodes
class CodeAnalyzer(ast.NodeVisitor …

Category: basics

Read More

Add-A-New-Column

Sun 29 June 2025

Add a New Column

import pandas as pd
data = {
    'city' : ['Toronto', 'Montreal', 'Waterloo'],
    'points' : [80, 70, 90]
}
data
{'city': ['Toronto', 'Montreal', 'Waterloo'], 'points': [80, 70, 90]}
type(data)
dict
df = pd.DataFrame(data)
df

Category: pandas-work

Read More

Age-Calculator

Sun 29 June 2025
from datetime import datetime
def get_age(d):
    d1 = datetime.now()
    months = (d1.year - d.year) * 12 + d1.month - d.month

    year = int(months / 12)
    return year
age = get_age(datetime(1991, 1, 1))
age
33


Score: 5

Category: basics

Read More

Binary-Linear-Timeit

Sun 29 June 2025
import timeit
def linear_search(lys, element):  
    for i in range (len(lys)):
        if lys[i] == element:
            return i
    return -1
def binary_search(lys, val):  
    first = 0
    last = len(lys)-1
    index = -1
    while (first <= last) and (index == -1):
        mid = (first+last)//2
        if lys[mid] == val:
            index = mid
        else:
            if …

Category: basics

Read More
Page 1 of 7

Next »