Last active
February 7, 2024 23:31
-
-
Save LizaPiya/188ae25abfd61106b0dfef2b5a30637a to your computer and use it in GitHub Desktop.
Functions
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 characters
| ## Write a function named same that takes a string as input, and simply returns that string. | |
| def same (string): | |
| return string |
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 characters
| ## Write a function called same_thing that returns the parameter, unchanged. | |
| def same_thing(a): | |
| return a |
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 characters
| ## Write a function called subtract_three that takes an integer or any number as input, and returns that number minus three. | |
| def subtract_three (a): | |
| return a-3 |
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 characters
| ## Write a function called change that takes one number as its input and returns that number, plus 7. | |
| def change(a): | |
| return a+7 |
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 characters
| ## Write a function called s_change that takes one string as input and returns that string, concatenated with the string ” for fun.”. | |
| def s_change(a): | |
| return a + " for fun." |
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 characters
| ## Write a function called decision that takes a string as input, and then checks the number of characters. If it has over 17 characters, return “This is a long string”, if it is shorter or has 17 characters, return “This is a short string”. | |
| def decision(s1): | |
| if len(s1) > 17: | |
| return ("This is a long string") | |
| else: | |
| return ("This is a short string") | |
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 characters
| ## Write a function named intro that takes a string as input. Given the string “Becky” as input, the function should return: “Hello, my name is Becky and I love SI 106.” | |
| def intro(str1): | |
| return("Hello, my name is "+ str1+ " and I love SI 106.") | |
| intro("Becky") #calling the function named intro | |
| #and passing the parameter Becky |
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 characters
| ## Write a function named total that takes a list of integers as input, and returns the total value of all those integers added together. | |
| def total(lst): | |
| tot=0 | |
| for i in lst: | |
| tot=tot+i | |
| return (tot) | |
| y=total([1,2,3,4,5,6,7]) |
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 characters
| ## Write a function called count that takes a list of numbers as input and returns a count of the number of elements in the list. | |
| def count(lst): | |
| values=0 | |
| for i in lst: | |
| values=values+1 | |
| return values | |
| y=count([1,2,3,4,5,6,7]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks