• Nov 19, 2022 •CodeCatch
0 likes • 1 view
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword" ) mycursor = mydb.cursor() mycursor.execute("CREATE DATABASE mydatabase")
def byte_size(s): return len(s.encode('utf-8')) byte_size('😀') # 4 byte_size('Hello World') # 11
• Nov 18, 2022 •AustinLeath
# importing the modules import os import shutil # getting the current working directory src_dir = os.getcwd() # printing current directory print(src_dir) # copying the files shutil.copyfile('test.txt', 'test.txt.copy2') #copy src to dst # printing the list of new files print(os.listdir())
0 likes • 8 views
#question1.py def rose(n) : if n==0 : yield [] else : for k in range(0,n) : for l in rose(k) : for r in rose(n-1-k) : yield [l]+[r]+[r] def start(n) : for x in rose(n) : print(x) #basically I am printing x for each rose(n) file print("starting program: \n") start(2) # here is where I call the start function
• Oct 15, 2022 •CodeCatch
1 like • 2 views
my_list = ["blue", "red", "green"] #1- Using sort or srted directly or with specifc keys my_list.sort() #sorts alphabetically or in an ascending order for numeric data my_list = sorted(my_list, key=len) #sorts the list based on the length of the strings from shortest to longest. # You can use reverse=True to flip the order #2- Using locale and functools import locale from functools import cmp_to_key my_list = sorted(my_list, key=cmp_to_key(locale.strcoll))
0 likes • 0 views
# Python code to demonstrate # method to remove i'th character # Naive Method # Initializing String test_str = "CodeCatch" # Printing original string print ("The original string is : " + test_str) # Removing char at pos 3 # using loop new_str = "" for i in range(len(test_str)): if i != 2: new_str = new_str + test_str[i] # Printing string after removal print ("The string after removal of i'th character : " + new_str)