Last active
October 6, 2024 17:59
-
-
Save cybersiddhu/5f02b7c3763bb0aaddfe2e9115d99d73 to your computer and use it in GitHub Desktop.
Revisions
-
cybersiddhu revised this gist
Oct 6, 2024 . 1 changed file with 1 addition and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -251,15 +251,6 @@ print(f"The sum of even numbers is: {even_sum}") 11. Create a function `find_common_elements` that takes two lists as input and returns a list of elements that are common to both input lists. Example input: `find_common_elements([1, 2, 3, 4, 5], [4, 5, 6, 7, 8])` 12. Create a function `remove_duplicates` that takes a list as input and returns a new list with duplicates removed, preserving the original order. Example input: `remove_duplicates([1, 2, 2, 3, 4, 4, 5])` -
cybersiddhu revised this gist
Oct 6, 2024 . 1 changed file with 19 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,24 @@ # Python Functions Tutorial ## Table of Contents 1. [Basic Concepts](#basic-concepts) 1. [Basic Function Definition and Calling](#1-basic-function-definition-and-calling) 2. [Function with Parameters](#2-function-with-parameters) 3. [Function with Return Value](#3-function-with-return-value) 4. [Function with Multiple Parameters](#4-function-with-multiple-parameters) 5. [Function with Default Parameter](#5-function-with-default-parameter) 2. [Intermediate Concepts](#intermediate-concepts) 1. [Function with Multiple Return Values](#1-function-with-multiple-return-values) 2. [Function with a List Parameter](#2-function-with-a-list-parameter) 3. [Function with Conditional Logic](#3-function-with-conditional-logic) 4. [Function with a Loop and Accumulator](#4-function-with-a-loop-and-accumulator) 3. [Exercises](#exercises) 1. [Basic Exercises](#basic-exercises) 2. [Intermediate Exercises](#intermediate-exercises) 3. [Challenge Exercises](#challenge-exercises) --- ## Basic Concepts ### 1. Basic Function Definition and Calling @@ -244,5 +263,3 @@ print(f"The sum of even numbers is: {even_sum}") 15. Create a function `remove_duplicates` that takes a list as input and returns a new list with duplicates removed, preserving the original order. Example input: `remove_duplicates([1, 2, 2, 3, 4, 4, 5])` -
cybersiddhu revised this gist
Oct 6, 2024 . 1 changed file with 34 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -193,43 +193,56 @@ print(f"The sum of even numbers is: {even_sum}") ## Exercises ### Basic Exercises 1. Create a function called `calculate_grade` that takes a student's score as a parameter. The function should return "A" for scores 90 and above, "B" for scores 80-89, "C" for 70-79, "D" for 60-69, and "F" for anything below 60. Example input: `calculate_grade(85)` 2. Write a function called `calculate_average` that takes a list of numbers as input and returns the average of those numbers. Example input: `calculate_average([80, 90, 70, 60, 85])` 3. Create a function `is_even` that takes a number as input and returns True if it's even, and False if it's odd. Example input: `is_even(7)` 4. Write a function `reverse_string` that takes a string as input and returns the reversed string. Example input: `reverse_string("Hello, World!")` 5. Create a function `count_vowels` that takes a string as input and returns the number of vowels (a, e, i, o, u) in the string. The function should be case-insensitive. Example input: `count_vowels("OpenAI")` ### Intermediate Exercises 6. Write a function `is_palindrome` that takes a string as input and returns True if it's a palindrome (reads the same forwards and backwards), and False otherwise. Ignore spaces and capitalization. Example input: `is_palindrome("A man a plan a canal Panama")` 7. Create a function `find_largest` that takes a list of numbers as input and returns the largest number in the list. Example input: `find_largest([5, 2, 9, 1, 7])` 8. Write a function `count_words` that takes a sentence as input and returns the number of words in the sentence. Example input: `count_words("The quick brown fox jumps over the lazy dog")` 9. Create a function `celsius_to_fahrenheit` that converts a temperature from Celsius to Fahrenheit. Example input: `celsius_to_fahrenheit(25)` 10. Write a function `generate_multiplication_table` that takes a number n as input and prints the multiplication table for that number up to 10. Example input: `generate_multiplication_table(7)` ### Challenge Exercises 11. Create a function `find_common_elements` that takes two lists as input and returns a list of elements that are common to both input lists. Example input: `find_common_elements([1, 2, 3, 4, 5], [4, 5, 6, 7, 8])` 12. Write a function `fibonacci` that takes a number n as input and returns the nth number in the Fibonacci sequence. Example input: `fibonacci(8)` 13. Create a function `caesar_cipher` that takes a string and a shift value as input, and returns the string encoded using the Caesar cipher technique. Only encrypt alphabetic characters. Example input: `caesar_cipher("Hello, World!", 3)` 14. Write a function `prime_factors` that takes a number as input and returns a list of its prime factors. Example input: `prime_factors(84)` 15. Create a function `remove_duplicates` that takes a list as input and returns a new list with duplicates removed, preserving the original order. Example input: `remove_duplicates([1, 2, 2, 3, 4, 4, 5])` These exercises cover a range of difficulty levels suitable for high school freshmen, allowing them to practice and expand their Python function writing skills. The examples provided give students a clear idea of how to use each function. -
cybersiddhu revised this gist
Oct 6, 2024 . 1 changed file with 35 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,8 @@ # Python Functions Tutorial ## Basic Concepts ### 1. Basic Function Definition and Calling ```python def say_hello(): @@ -8,13 +12,13 @@ def say_hello(): say_hello() ``` **Explanation:** - We define a function called `say_hello` using the `def` keyword. - The function doesn't take any parameters (that's why the parentheses are empty). - Inside the function, we have one line of code that prints a message. - To use (or "call") the function, we write its name followed by parentheses. ### 2. Function with Parameters ```python def greet(name): @@ -25,13 +29,13 @@ greet("Alice") greet("Bob") ``` **Explanation:** - This function, `greet`, takes one parameter called `name`. - When we call the function, we provide a value for `name` inside the parentheses. - The function uses this `name` to create a personalized greeting. - We can call the function multiple times with different names. ### 3. Function with Return Value ```python def add_five(number): @@ -43,13 +47,13 @@ answer = add_five(10) print(answer) # This will print 15 ``` **Explanation:** - This function `add_five` takes a number as input. - It adds 5 to this number and stores it in `result`. - The `return` statement sends this result back to wherever the function was called. - When we use the function, we can store its returned value in a variable (like `answer`). ### 4. Function with Multiple Parameters ```python def calculate_rectangle_area(length, width): @@ -61,13 +65,13 @@ rectangle1_area = calculate_rectangle_area(5, 3) print(f"The area of the rectangle is: {rectangle1_area}") ``` **Explanation:** - This function takes two parameters: `length` and `width`. - It multiplies these values to calculate the area of a rectangle. - The calculated area is then returned. - When calling the function, we need to provide two values, one for each parameter. ### 5. Function with Default Parameter ```python def greet_with_title(name, title="Mr."): @@ -78,20 +82,17 @@ greet_with_title("Smith") greet_with_title("Johnson", "Dr.") ``` **Explanation:** - This function has two parameters: `name` and `title`. - `title` has a default value of "Mr.". This means if we don't provide a title, it will use "Mr.". - In the first function call, we only provide the name, so it uses the default title. - In the second call, we provide both a name and a title, overriding the default. --- ## Intermediate Concepts ### 1. Function with Multiple Return Values ```python def get_name_parts(full_name): @@ -106,14 +107,14 @@ print(f"First name: {first}") print(f"Last name: {last}") ``` **Explanation:** - This function takes a full name as input. - It uses the `split()` method to separate the name into parts. - It then assigns the first part to `first_name` and the last part to `last_name`. - The function returns both of these values. - When calling the function, we can assign both returned values to separate variables at once. ### 2. Function with a List Parameter ```python def find_longest_word(word_list): @@ -129,14 +130,14 @@ longest = find_longest_word(words) print(f"The longest word is: {longest}") ``` **Explanation:** - This function takes a list of words as input. - It uses a for loop to go through each word in the list. - It compares the length of each word with the current longest word. - If a longer word is found, it becomes the new longest word. - After checking all words, the function returns the longest one found. ### 3. Function with Conditional Logic ```python def classify_temperature(temp): @@ -157,14 +158,14 @@ classification = classify_temperature(temperature) print(f"{temperature} degrees is classified as: {classification}") ``` **Explanation:** - This function takes a temperature value as input. - It uses a series of if-elif-else statements to classify the temperature. - Each condition checks if the temperature is below a certain threshold. - The function returns the appropriate classification as a string. - When we call the function, it evaluates the input and returns the classification. ### 4. Function with a Loop and Accumulator ```python def sum_even_numbers(numbers): @@ -180,18 +181,27 @@ even_sum = sum_even_numbers(number_list) print(f"The sum of even numbers is: {even_sum}") ``` **Explanation:** - This function takes a list of numbers as input. - It initializes a `total` variable to keep track of the sum. - It loops through each number in the list. - For each number, it checks if it's even by using the modulo operator (%). - If the number is even, it's added to the total. - After checking all numbers, the function returns the total sum of even numbers. --- ## Exercises ### Basic Exercise Create a function called `calculate_grade` that takes a student's score as a parameter. The function should return "A" for scores 90 and above, "B" for scores 80-89, "C" for 70-79, "D" for 60-69, and "F" for anything below 60. ### Intermediate Exercise Create a function called `count_vowels` that takes a string as input and returns the number of vowels (a, e, i, o, u) in the string. The function should be case-insensitive, meaning it should count both uppercase and lowercase vowels. ### Additional Exercises 1. Write a function called `calculate_average` that takes a list of numbers as input and returns the average of those numbers. @@ -223,4 +233,3 @@ Additional Exercises: 15. Write a function `validate_email` that takes a string as input and returns True if it's a valid email address, and False otherwise. -
cybersiddhu revised this gist
Oct 6, 2024 . 1 changed file with 33 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -191,4 +191,36 @@ Explanation: Exercise: Create a function called `count_vowels` that takes a string as input and returns the number of vowels (a, e, i, o, u) in the string. The function should be case-insensitive, meaning it should count both uppercase and lowercase vowels. Additional Exercises: 1. Write a function called `calculate_average` that takes a list of numbers as input and returns the average of those numbers. 2. Create a function `is_palindrome` that takes a string as input and returns True if it's a palindrome (reads the same forwards and backwards), and False otherwise. Ignore spaces and capitalization. 3. Write a function `fibonacci` that takes a number n as input and returns the nth number in the Fibonacci sequence. 4. Create a function `prime_factors` that takes a number as input and returns a list of its prime factors. 5. Write a function `reverse_words` that takes a sentence as input and returns the sentence with each word reversed, but the order of words in the sentence remains the same. 6. Create a function `caesar_cipher` that takes a string and a shift value as input, and returns the string encoded using the Caesar cipher technique. 7. Write a function `binary_to_decimal` that converts a binary number (given as a string) to its decimal equivalent. 8. Create a function `find_common_elements` that takes two lists as input and returns a list of elements that are common to both input lists. 9. Write a function `is_anagram` that takes two strings as input and returns True if they are anagrams (contain the same letters in different orders), and False otherwise. 10. Create a function `generate_password` that takes a length as input and generates a random password of that length, including uppercase and lowercase letters, numbers, and special characters. 11. Write a function `flatten_list` that takes a nested list as input and returns a flattened version of the list. 12. Create a function `word_frequency` that takes a string as input and returns a dictionary with words as keys and their frequencies as values. 13. Write a function `remove_duplicates` that takes a list as input and returns a new list with duplicates removed, preserving the original order. 14. Create a function `matrix_transpose` that takes a 2D list (matrix) as input and returns its transpose. 15. Write a function `validate_email` that takes a string as input and returns True if it's a valid email address, and False otherwise. These exercises cover a range of difficulty levels and concepts, allowing students to practice and expand their Python function writing skills. -
cybersiddhu revised this gist
Sep 2, 2024 . 1 changed file with 156 additions and 49 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,87 +1,194 @@ 1. Basic Function Definition and Calling ```python def say_hello(): print("Hello, friend!") # Using the function say_hello() ``` Explanation: - We define a function called `say_hello` using the `def` keyword. - The function doesn't take any parameters (that's why the parentheses are empty). - Inside the function, we have one line of code that prints a message. - To use (or "call") the function, we write its name followed by parentheses. 2. Function with Parameters ```python def greet(name): print(f"Hello, {name}!") # Using the function greet("Alice") greet("Bob") ``` Explanation: - This function, `greet`, takes one parameter called `name`. - When we call the function, we provide a value for `name` inside the parentheses. - The function uses this `name` to create a personalized greeting. - We can call the function multiple times with different names. 3. Function with Return Value ```python def add_five(number): result = number + 5 return result # Using the function answer = add_five(10) print(answer) # This will print 15 ``` Explanation: - This function `add_five` takes a number as input. - It adds 5 to this number and stores it in `result`. - The `return` statement sends this result back to wherever the function was called. - When we use the function, we can store its returned value in a variable (like `answer`). 4. Function with Multiple Parameters ```python def calculate_rectangle_area(length, width): area = length * width return area # Using the function rectangle1_area = calculate_rectangle_area(5, 3) print(f"The area of the rectangle is: {rectangle1_area}") ``` Explanation: - This function takes two parameters: `length` and `width`. - It multiplies these values to calculate the area of a rectangle. - The calculated area is then returned. - When calling the function, we need to provide two values, one for each parameter. 5. Function with Default Parameter ```python def greet_with_title(name, title="Mr."): print(f"Hello, {title} {name}!") # Using the function greet_with_title("Smith") greet_with_title("Johnson", "Dr.") ``` Explanation: - This function has two parameters: `name` and `title`. - `title` has a default value of "Mr.". This means if we don't provide a title, it will use "Mr.". - In the first function call, we only provide the name, so it uses the default title. - In the second call, we provide both a name and a title, overriding the default. Exercise: Create a function called `calculate_grade` that takes a student's score as a parameter. The function should return "A" for scores 90 and above, "B" for scores 80-89, "C" for 70-79, "D" for 60-69, and "F" for anything below 60. These examples and explanations should be more suitable for high school freshmen. They introduce basic concepts of functions step-by-step, with clear explanations of what each part does. The exercise at the end allows students to apply what they've learned in a practical scenario. --------------------------------------------------------- 1. Function with Multiple Return Values ```python def get_name_parts(full_name): parts = full_name.split() first_name = parts[0] last_name = parts[-1] return first_name, last_name # Using the function first, last = get_name_parts("John Doe Smith") print(f"First name: {first}") print(f"Last name: {last}") ``` Explanation: - This function takes a full name as input. - It uses the `split()` method to separate the name into parts. - It then assigns the first part to `first_name` and the last part to `last_name`. - The function returns both of these values. - When calling the function, we can assign both returned values to separate variables at once. 2. Function with a List Parameter ```python def find_longest_word(word_list): longest_word = "" for word in word_list: if len(word) > len(longest_word): longest_word = word return longest_word # Using the function words = ["apple", "banana", "cherry", "date", "elderberry"] longest = find_longest_word(words) print(f"The longest word is: {longest}") ``` Explanation: - This function takes a list of words as input. - It uses a for loop to go through each word in the list. - It compares the length of each word with the current longest word. - If a longer word is found, it becomes the new longest word. - After checking all words, the function returns the longest one found. 3. Function with Conditional Logic ```python def classify_temperature(temp): if temp < 0: return "Freezing" elif temp < 10: return "Cold" elif temp < 20: return "Cool" elif temp < 30: return "Warm" else: return "Hot" # Using the function temperature = 25 classification = classify_temperature(temperature) print(f"{temperature} degrees is classified as: {classification}") ``` Explanation: - This function takes a temperature value as input. - It uses a series of if-elif-else statements to classify the temperature. - Each condition checks if the temperature is below a certain threshold. - The function returns the appropriate classification as a string. - When we call the function, it evaluates the input and returns the classification. 4. Function with a Loop and Accumulator ```python def sum_even_numbers(numbers): total = 0 for num in numbers: if num % 2 == 0: # Check if the number is even total += num return total # Using the function number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even_sum = sum_even_numbers(number_list) print(f"The sum of even numbers is: {even_sum}") ``` Explanation: - This function takes a list of numbers as input. - It initializes a `total` variable to keep track of the sum. - It loops through each number in the list. - For each number, it checks if it's even by using the modulo operator (%). - If the number is even, it's added to the total. - After checking all numbers, the function returns the total sum of even numbers. Exercise: Create a function called `count_vowels` that takes a string as input and returns the number of vowels (a, e, i, o, u) in the string. The function should be case-insensitive, meaning it should count both uppercase and lowercase vowels. These examples introduce slightly more complex concepts like working with lists, using loops within functions, and implementing more detailed logic. The explanations break down each part of the function to help students understand what's happening step by step. The exercise at the end challenges students to apply these concepts in a new context. -
cybersiddhu revised this gist
Sep 2, 2024 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,3 @@ Course: Introduction to Python Functions Lesson 1: What is a Function? -
cybersiddhu created this gist
Sep 2, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,89 @@ Certainly! I'd be happy to create a small course on Python functions for high school freshmen. Let's break it down into manageable sections with examples and exercises. Course: Introduction to Python Functions Lesson 1: What is a Function? A function is a reusable block of code that performs a specific task. It helps us organize our code and avoid repetition. Example: ```python def greet(): print("Hello, world!") greet() # Calling the function ``` Exercise 1: Create a function called `introduce_yourself` that prints your name and age. Lesson 2: Functions with Parameters Functions can accept input values called parameters, which allow them to work with different data. Example: ```python def greet_person(name): print(f"Hello, {name}!") greet_person("Alice") greet_person("Bob") ``` Exercise 2: Create a function called `calculate_area` that takes the length and width of a rectangle and prints its area. Lesson 3: Return Values Functions can send back results using the `return` statement. Example: ```python def add_numbers(a, b): return a + b result = add_numbers(5, 3) print(result) # Output: 8 ``` Exercise 3: Create a function called `celsius_to_fahrenheit` that converts a temperature from Celsius to Fahrenheit and returns the result. Lesson 4: Default Parameters You can set default values for parameters, which are used when no argument is provided. Example: ```python def greet_with_title(name, title="Mr."): print(f"Hello, {title} {name}!") greet_with_title("Smith") greet_with_title("Johnson", "Dr.") ``` Exercise 4: Create a function called `power` that takes two parameters: a number and an exponent (with a default value of 2). The function should return the number raised to the given exponent. Lesson 5: Multiple Parameters and Return Values Functions can have multiple parameters and return multiple values. Example: ```python def calculate_stats(numbers): total = sum(numbers) average = total / len(numbers) return total, average sum_result, avg_result = calculate_stats([1, 2, 3, 4, 5]) print(f"Sum: {sum_result}, Average: {avg_result}") ``` Exercise 5: Create a function called `min_max` that takes a list of numbers and returns both the minimum and maximum values from the list. Final Project: Create a simple calculator program that uses functions for addition, subtraction, multiplication, and division. The program should: 1. Ask the user to choose an operation 2. Ask for two numbers 3. Call the appropriate function 4. Display the result This course provides a basic introduction to Python functions with examples and exercises. Each lesson builds upon the previous one, gradually introducing more complex concepts. The final project allows s tudents to apply what they've learned in a practical scenario.