From 3140a0f574c5f0c2536a0c9b996bb136bcc77d9e Mon Sep 17 00:00:00 2001 From: Jisha Ershad Date: Sun, 11 Dec 2022 20:36:30 -0500 Subject: [PATCH 1/6] jisha updated --- 12_09_practice.py | 133 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 126 insertions(+), 7 deletions(-) diff --git a/12_09_practice.py b/12_09_practice.py index 7914563..0326e1a 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -1,29 +1,148 @@ #Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. +list1=[-1, 3, 5, -5] + +def make_it_big(list1): + list=[] + for i,a in enumerate(list1): + if a < 0: + list.append("big") + else: + list.append(a) + print(list) +make_it_big(list1) + #Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). -#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 +list1=[-1,1,1,1] -#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 +def count_positives(list1): + list=[] + sum=0 + for i,a in enumerate(list1): + if a < 0: + list.append(a) + else: + sum+=a + list.append(a) + list[-1]=sum + print(list) +count_positives(list1) +#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 +list1=[1,2,3,4] +def sum_total(list1): + sum=0 + for i in list1: + sum+=i + print (sum) + return sum +sum_total(list1) +#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 +list1=[1,2,3,4] +def average(list1): + sum=0 + for i in list1: + sum+=i + avg1=sum/len(list1) + print (avg1) + return avg1 +average(list1) #Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 - +list1=[1,2,3,4] +def length(list1): + count=0 + for i in list1: + count+=1 + print (count) + return count +length(list1) #Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. -# +list1=[0,-3, 5,2] +def minimum(list1): + if len(list1)==0: + print("False") + else: + return min(list1) + print(min(list1)) +minimum(list1) #Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. - +list1=[0,-3, 5,2] +def maximum(list1): + if len(list1)==0: + print("False") + else: + return max(list1) + print(max(list1)) +maximum(list1) #Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. - +lis1=[0,-3, 5,2] +def ultimateaalyze(list1): + dict={ + "sum_total":sum_total(list1), + "average":average(list1), + "minimum":minimum(list1), + "maximum":maximum(list1), + "length":length(list1) + } + print(dict) +ultimateaalyze(list1) + #ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. +list1=[1,2,3,4] +def reverse(list1): + print (list1[::-1]) + return list1[::-1] +reverse(list1) #Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome. +#string1="radar" +def ispalindrome(string1): + if string1==string1[::-1]: + print(f"{string1} is palindrome") + else: + print(f"{string1} is not palindrome") +string1 = input('Enter any string ') +ispalindrome(string1) + #Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: #If the number is a multiple of 3, print “Fizz” instead of the number. #If the number is a multiple of 5, print “Buzz” instead of the number. #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number. +def fizzbuzz(): + for num in range(0,101): + if num%3==0 and num%5==0: + print("FizzBuzz") + elif num%3== 0: + print("Fizz") + elif num%5==0: + print("Buzz") + else: + print(num) +fizzbuzz() + #Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, #F(0) = 0, F(1) = 1 #F(n) = F(n - 1) + F(n - 2), for n > 1. - #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. \ No newline at end of file + #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. + +def fibonacci(num): + num1 = 0 + num2 = 1 + seq = 0 + if num == 0: + return 0 + elif num== 1: + return 1 + for i in range(num): + print(seq, end=' ') + num1 = num2 + num2 = seq + seq = num1 + num2 + +num = int(input('Enter any number ')) +fibonacci(num) + + From d61721d9230523f8298182147cf308b57d46a77a Mon Sep 17 00:00:00 2001 From: gitjish <115896761+gitjish@users.noreply.github.com> Date: Tue, 13 Dec 2022 02:05:05 -0500 Subject: [PATCH 2/6] Add files via upload --- 12_12_pythonpractice.py | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 12_12_pythonpractice.py diff --git a/12_12_pythonpractice.py b/12_12_pythonpractice.py new file mode 100644 index 0000000..d6cbb4a --- /dev/null +++ b/12_12_pythonpractice.py @@ -0,0 +1,77 @@ +#1. Use list comprehension to create a list containing a solution for the +#famous FizzBuzz problem. For integers 1 to 100, inclusively, the value +#should be: +# 'Fizz' if divisible by 3 +# 'Buzz' if divisible by 5 +# 'FizzBuzz' if divisible by both 3 and 5 +# The integer itself if not divisible by both 3 and 5 +list2=['FizzBuzz' if (num%3==0 and num%5==0) else "Fizz" if (num%3==0)else "Buzz"if (num%5==0) else num for num in range(1,101)] +print(list2) + +#1. Create a generator, primes_gen that generates prime numbers +#starting from 2. +def primes_gen(): + i = 2 + while i < 100 : + prime = True # reset the `prime` variable before the inner loop + for a in range(2, i): + if i%a == 0: + prime = False + break + if prime: + yield i + i += 1 # don't forget to advance `i` + +gen = primes_gen() +for _ in range(10): + print(next(gen), end=' ') +print(end='\n') +#2. Create a generator, unique_letters that generates unique letters from +#the input string. It should generate the letters in the same order as +#from the input string. +def unique_letters(str1): + unique="" + for i in str1: + if i not in unique: + unique=unique+' '+i + yield unique +for letter in unique_letters('hello'): + print(letter, end=' ') +print(end='\n') + +#Consider the list: + +#1. Sort the list by each language's version in ascending order. +prog_lang = [('Python', 3.8), +('Java', 13), +('JavaScript', 2019), +('Scala', 2.13)] +list1=prog_lang +list1.sort(key=lambda x: x[1]) +print(list1) +#2. Sort the list by the length of the name of each language in descending +##order. +list2=prog_lang +list2.sort(key=lambda x: len(x[0]),reverse=True) +print(list2) +#3. Filter the list so that it only contains languages with 'a' in it. +prog_lang = [('Python', 3.8), +('Java', 13), +('JavaScript', 2019), +('Scala', 2.13)] +list3=list(filter(lambda x: 'a' in x[0], prog_lang)) +print(list3) +#4. Filter the list so that it only contains languages whose version is in int +list4=list(filter(lambda x: type(x[1])== int, prog_lang)) +print(list4) +#5. Transform the list so that it contains the tuples in the form, +#("language in all lower case", length of the language string) +list5=list(map(lambda x : (x[0].lower(),len(x[0])), prog_lang)) +print(list5) +#6. Generate a tuple in theform, +#("All languages separated by commas", +#"All versions separated by commas") +list6=list(map(lambda x : x[0]+' ,'+str(x[1]),prog_lang)) +print(list6) + + From aa8706d50ae59bf80b467b8782ffee9fe5be1432 Mon Sep 17 00:00:00 2001 From: Jisha Ershad Date: Tue, 13 Dec 2022 12:01:03 -0500 Subject: [PATCH 3/6] practice problems on generator,lamda --- 12_12_pythonpractice.py | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 12_12_pythonpractice.py diff --git a/12_12_pythonpractice.py b/12_12_pythonpractice.py new file mode 100644 index 0000000..5103791 --- /dev/null +++ b/12_12_pythonpractice.py @@ -0,0 +1,77 @@ +#1. Use list comprehension to create a list containing a solution for the +#famous FizzBuzz problem. For integers 1 to 100, inclusively, the value +#should be: +# 'Fizz' if divisible by 3 +# 'Buzz' if divisible by 5 +# 'FizzBuzz' if divisible by both 3 and 5 +# The integer itself if not divisible by both 3 and 5 +list2=['FizzBuzz' if (num%3==0 and num%5==0) else "Fizz" if (num%3==0)else "Buzz"if (num%5==0) else num for num in range(1,101)] +print(list2) + +#1. Create a generator, primes_gen that generates prime numbers +#starting from 2. +def primes_gen(): + i = 2 + while i < 100 : + prime = True # reset the `prime` variable before the inner loop + for a in range(2, i): + if i%a == 0: + prime = False + break + if prime: + yield i + i += 1 # don't forget to advance `i` + +gen = primes_gen() +for _ in range(10): + print(next(gen), end=' ') +print(end='\n') +#2. Create a generator, unique_letters that generates unique letters from +#the input string. It should generate the letters in the same order as +#from the input string. +def unique_letters(str1): + unique="" + for i in str1: + if i not in unique: + unique=unique+' '+i + yield unique +for letter in unique_letters('hello'): + print(letter, end=' ') +print(end='\n') + +#Consider the list: + +#1. Sort the list by each language's version in ascending order. +prog_lang = [('Python', 3.8), +('Java', 13), +('JavaScript', 2019), +('Scala', 2.13)] +list1=prog_lang +list1.sort(key=lambda x: x[1]) +print(list1) +#2. Sort the list by the length of the name of each language in descending +##order. +list2=prog_lang +list2.sort(key=lambda x: len(x[0]),reverse=True) +print(list2) +#3. Filter the list so that it only contains languages with 'a' in it. +prog_lang = [('Python', 3.8), +('Java', 13), +('JavaScript', 2019), +('Scala', 2.13)] +list3=list(filter(lambda x: 'a' in x[0], prog_lang)) +print(list3) +#4. Filter the list so that it only contains languages whose version is in int +list4=list(filter(lambda x: type(x[1])== int, prog_lang)) +print(list4) +#5. Transform the list so that it contains the tuples in the form, +#("language in all lower case", length of the language string) +list5=list(map(lambda x : (x[0].lower(),len(x[0])), prog_lang)) +print(list5) +#6. Generate a tuple in theform, +#("All languages separated by commas", +#"All versions separated by commas") +list6=list(map(lambda x : x[0]+' ,'+str(x[1]),prog_lang)) +print(list6) + + From d8e2b7cc0874154dc498b22b2d803faf52537742 Mon Sep 17 00:00:00 2001 From: Jisha Ershad Date: Tue, 13 Dec 2022 22:25:21 -0500 Subject: [PATCH 4/6] practice on Generator,lamda updated --- #dec12list comprehension.py | 33 +++++++++++++++++++++++++++++++++ 12_12_pythonpractice.py | 7 +++++-- 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 #dec12list comprehension.py diff --git a/#dec12list comprehension.py b/#dec12list comprehension.py new file mode 100644 index 0000000..0d759a8 --- /dev/null +++ b/#dec12list comprehension.py @@ -0,0 +1,33 @@ +#list comprehension +expression for variable in sequence (optional predicate) +my_list=[] +for i in range(1,10,2): + my_list.append(i**2) + +my_list2=[i**2 for i in range(1,10,2)] +print(my_list) +print(my_list2) + +odd_square=[] +for x in range(1,11): + if x % 2==1: + odd_square.append(x**2) + +odd_square2 = [x**2 for x in range(1,11) if x %2 ==1] + print(odd_square2) + + +def dec(func, n): + def wrapper(): + for _ in range(n): + func() + return wrapper + +@dec +('say_hello', 5) +def say_hello(): + print('hello') + +#say_hello = dec(say_hello, 5) +say_hello() + diff --git a/12_12_pythonpractice.py b/12_12_pythonpractice.py index 5103791..44b666b 100644 --- a/12_12_pythonpractice.py +++ b/12_12_pythonpractice.py @@ -1,3 +1,5 @@ +#practice on Generator,lamda +from functools import reduce #1. Use list comprehension to create a list containing a solution for the #famous FizzBuzz problem. For integers 1 to 100, inclusively, the value #should be: @@ -71,7 +73,8 @@ def unique_letters(str1): #6. Generate a tuple in theform, #("All languages separated by commas", #"All versions separated by commas") -list6=list(map(lambda x : x[0]+' ,'+str(x[1]),prog_lang)) -print(list6) +print(reduce(lambda x,y :(f'{x[0]},{y[0]}',f'{x[1]},{y[1]}'),prog_lang)) + + From 0322bac598c254282f5d4404e5bc026ed231e41d Mon Sep 17 00:00:00 2001 From: Jisha Ershad Date: Tue, 3 Jan 2023 11:41:41 -0500 Subject: [PATCH 5/6] I ran a connection --- newdatabase.ipynb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/newdatabase.ipynb b/newdatabase.ipynb index 8e2058e..071ee82 100644 --- a/newdatabase.ipynb +++ b/newdatabase.ipynb @@ -32,6 +32,38 @@ "print(\"Connecting to Mariadb database...\")\n", " " ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "cur=con.cursor()\n", + "st= \"SELECT productLine,COUNT(*)\\\n", + " FROM products\\\n", + " WHERE productLine='{}'\"\n", + "cur.execute(st.format('Motorcycles'))\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Motorcycles', 13)]\n" + ] + } + ], + "source": [ + "result=cur.fetchall()\n", + "print(result)" + ] } ], "metadata": { From 36c67a584a1ea0af9562eee4775523f670608777 Mon Sep 17 00:00:00 2001 From: Jisha Ershad Date: Tue, 3 Jan 2023 13:42:44 -0500 Subject: [PATCH 6/6] insert,update practised --- newdatabase.ipynb | 168 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 163 insertions(+), 5 deletions(-) diff --git a/newdatabase.ipynb b/newdatabase.ipynb index 071ee82..c384c2e 100644 --- a/newdatabase.ipynb +++ b/newdatabase.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 4, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -49,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -62,8 +62,166 @@ ], "source": [ "result=cur.fetchall()\n", - "print(result)" + "print(result)\n", + "con.close()" ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "now create a new database\n", + "we are not connecting to a specific database\n", + "strings can be passed directly into execute method" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connecting to Mariadb database...\n", + "test_db created\n" + ] + } + ], + "source": [ + "con = mariadb.connect(\n", + " host=\"localhost\",\n", + " user=\"root\",\n", + " password=\"password\"\n", + "\n", + ")\n", + "print(\"Connecting to Mariadb database...\")\n", + "cur=con.cursor()\n", + "st=\"CREATE DATABASE test_db\"\n", + "cur.execute(st)\n", + "print(\"test_db created\")\n", + "con.close()\n", + " " + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "create table to test_db" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connecting to Mariadb database...\n", + "Registration table created\n" + ] + } + ], + "source": [ + "con = mariadb.connect(\n", + " host=\"localhost\",\n", + " user=\"root\",\n", + " password=\"password\",\n", + " database=\"test_db\"\n", + "\n", + ")\n", + "print(\"Connecting to Mariadb database...\")\n", + "cur=con.cursor()\n", + "st=\"CREATE TABLE Registration\\\n", + " (id INTEGER,age INTEGER,\\\n", + " first VARCHAR(50),last VARCHAR(50))\"\n", + "cur.execute(st)\n", + "print(\"Registration table created\")\n", + "con.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connecting to Mariadb database...\n", + "Values are inserted\n" + ] + } + ], + "source": [ + "con = mariadb.connect(\n", + " host=\"localhost\",\n", + " user=\"root\",\n", + " password=\"password\",\n", + " database=\"test_db\"\n", + "\n", + ")\n", + "print(\"Connecting to Mariadb database...\")\n", + "cur=con.cursor()\n", + "st=\"INSERT INTO Registration \\\n", + " (id, age, first, last) VALUES \\\n", + " (100, 29, 'Michael', 'Jordan'), \\\n", + " (101, 29, 'Diego', 'Maradona'), \\\n", + " (102, 33, 'Babe', 'Ruth'), \\\n", + " (103, 40, 'Wayne', 'Gretzky'), \\\n", + " (104, 27, 'Michelle', 'Kwan'), \\\n", + " (105, 35, 'Steffi', 'Graf')\"\n", + "cur.execute(st)\n", + "con.commit()\n", + "print(\"Values are inserted\") \n", + "con.close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Connecting to Mariadb database...\n", + "Values are Updated\n" + ] + } + ], + "source": [ + "con = mariadb.connect(\n", + " host=\"localhost\",\n", + " user=\"root\",\n", + " password=\"password\",\n", + " database=\"test_db\"\n", + "\n", + ")\n", + "print(\"Connecting to Mariadb database...\")\n", + "cur=con.cursor()\n", + "st=\"UPDATE Registration\\\n", + " SET age=30 WHERE id IN (100,101)\"\n", + "cur.execute(st)\n", + "con.commit()\n", + "print(\"Values are Updated\") \n", + "con.close() " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {