Python Functions || Python Tutorial || Learn Python Programming
- June 18, 2024
- Posted by: MainInstructor
- Category: Python
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2024/04/1713234983_hqdefault.jpg?resize=480%2C360&ssl=1)
Video Title: Python Functions || Python Tutorial || Learn Python Programming
When programming, you will often encounter calculations and logical operations you need to perform repeatedly. One way to handle this is to write the same code over, and over, and over, and over, and — … — over… A better solution is to write a function. Functions enable you to reuse logic an infinite number of times without repeating yourself. This is especially helpful to avoid moments of deja vu. A better solution is to write a function. Functions enable you to reuse logic an infinite number of times without repeating yourself. This is especially helpful to avoid moments of deja vu… Time to begin… Before we write a function of our own, call the directory function to see what objects are available to us… When you first start the Python interpreter, you will see a list of 4 standard attributes. Once you define a function, it will appear in the directory. To define a function, you write “d-e-f” followed by the name of the function. We will call this function F. Next, you write parentheses. Inside the parentheses you list the inputs to the function. Another name for “inputs” is “arguments”. This function has zero arguments. Next, type a “colon” and press return. The colon is how you start a new code block in Python. For our first function we will keep things simple. We will pass. The word “pass” is how you tell Python to skip this line and do nothing. Notice that we indented the code inside the function. In Python, you group code by indentation. There. We have created our first function. It is short. It is simple. But does it work? To call the function, type the name and parentheses… The function F did nothing, just like we told it to. By the way, look what happens if you forget to type parentheses. Without parentheses, Python displays that F is a function, and gives the memory address. Interesting, but not very helpful. You need to include parentheses to actually call the function. If you call the directory function again, you will see that F is now listed. Now we will write a function that actually does something. We start with d-e-f. We will name this function ping. Next, list the arguments inside parentheses. This function will have zero arguments. Finally, type colon and press enter. Functions in Python can return values. To return a value, type the word “return” and then the object. This function will return the string “Ping”. To show our enthusiasm, we will type an exclamation point. Return values are optional; you are under no obligation to return something. Now call the function. This function returns the string ‘Ping’. Since we did not assign this to a variable, Python printed it to the terminal. But we can also store the return value to a variable. To see that this worked, print the value X. Call the directory function again and you will see the function Ping, and the variable X listed. For our next example, recall the the volume of a sphere is: V = 4/3 π r3, where R is the radius of the sphere. We will write a function which will return the volume of a sphere when given the radius. To do this, we will need to use the number Pi. This is available in Python, but first you must import the math module. If you display the directory of the math module, you will see Pi listed. You can now access the number pi. We are ready to define the volume function. Type ‘def’ then the name of the function. We will call this function ‘volume’. This function will have a single argument: the radius R… Next, we will write a brief comment describing this function. This is called a “docstring” and provides documentation on what the function does and how to use it. Next, we compute the volume. Notice that we used decimals for the fraction 4/3. This is because in Python 2, if you divide one integer by another, it returns the quotient, not the exact value. Also, notice that you use a double asterisk for exponents. Finally, return the volume. Let’s test this function. Compute the volume of a sphere with radius 2. It works… Because we used an argument when defining the function, you must provide an input when calling it — R is a required argument. Look what happens if you call ‘volume’ without an argument — you get an error and a reminder to use an argument. We can use the help function to see how to use the volume function… Very helpful… We have created functions with no arguments and with one argument. We will now throw caution into the vacuum of space and write a function with two arguments. Our function will compute the area of a triangle. Recall that the area of a triangle is ½ base x height. Our previous function was named “volume”, which was somewhat vague. The name does not tell you what shape is being considered. This time, we will be more explicit and name the function “triangle area”. To compute the area, we need two arguments: the base B and the height H. Write a docstring giving a brief description of this function… And finally, return the area of the triangle. We are ready to test the function. The area of a triangle with base 3 and height 6 is … 9. This is correct. There is no limit to how many arguments you can use in your function, but if the number of inputs is too large, you will alienate others and receive accusing glares… Functions in Python can accept another kind of argument called “keyword arguments”. To show how to use these kinds of arguments, we will write a function which converts a person’s height from American Units (feet and inches) to centimeters. Recall that 1 inch equals 2.54 cm, and 1 foot equals 12 inches. Call this function cm, for centimeter. This function will accept two arguments: feet and inches. Notice that these arguments have equal signs after them. It looks as if we are assigning values to these arguments. In fact, we are. We are assigning a default value of 0 to each argument. For this reason, Python also calls keyword arguments “default arguments”… Next, write a docstring describing the function. Notice that it is customary to use triple-quotes for a docstring. Now for the actual code. We first convert inches to centimeters… Then we convert feet to centimeters… And we return the combined value. We could have combined these all on one line, but it is better to write clean code which is easy to read, as opposed to compact code which impresses no one. Here is how you call a function with keyword arguments. First we convert 5 feet to centimeters… Next we convert 70 inches to centimeters… And lastly, convert 5 feet 8 inches to centimeters… We can also perform this last calculation by specifying inches first… Keyword arguments help you to write flexible functions and clean code. There are 2 kinds of arguments you can use when writing a function. A keyword argument, which has an equal sign, and a required argument which does not have an equal sign. When writing a function, you can use both kinds of arguments. But if you do this, the keyword arguments must come last. For example, if you define a function G with a keyword argument first, you get a Syntax Error. Notice that Python calls these “default arguments”. This is another name for a keyword argument. To fix this, you have to list the non-default arguments first. These are also called required arguments since they are required. To call this function, you must pass in a value for the required argument Y. The keyword argument X is optional. If you do not provide a value for X, the default value is used. If you want to pass in a value for the keyword argument, then you must specify it by its name. Required arguments are not given a name. They are determined by their position. Functions in Python are flexible contraptions. Required arguments, keyword arguments, docstrings, return values — together they empower you to write some amazing, reusable code. And if your function does not require an input, you will not get an argument from me…
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Wear a Socratica Python shirt for good luck coding: https://shop.socratica.com/products/python-by-socratica
i did everything line by line and I'm still lost
Wow, I've watched a bunch of tuts on functions and they only ever managed to confuse me further. I get it now 😭 The explanations are really helpful and to the point, I write everything down in the comments as I code along. Nice one!
Quite methodical – Thanks. I´m new to all of this, so unless it´s not going to be as simple as this, I may as well start learning Chinese! Thanks.
Lovely, Socratica; and so much for "enthusiasm" under the "Ping!" display.😂😂
ok that was amazing
If I was learning this with no knowledge of python, talking straight out of the blocks about a (defined, inbuilt) directory function dir() in the same breath as talking about how one goes about defining a function would be quite confusing. The natural question from someone new to python would be:
"So do you need to define functions or not. The dir() function didn't seem to be defined at all while the other one needed to be. Are some of these functions built-in then. Why didn't she say this?"
Also, using the dir() function as a first function to motivate the notion of a function doesn't seem such a great idea. One is left thinking "so we use functions to show us where functions are? It feels pointless. But what can functions actually do?"
1:07 surely it's parameters that are inside the parentheses when defining the function, and not arguments.
powerful and simple explanations. just one thing though: I wish she could smile when explaining things. lol
is this a real person or a very efficiently used deep fake ?
l am sure Socratica can speak french. She is always using french's words <3
I have to love my heart out on your videos. 😘😍🥰❤️♥️💙💜💗🖤💛💖💞💚❣️💓😻🧡💘💝👩❤️👨💌💟💏👩❤️💋👩👩❤️💋👨💑👩❤️👩👨❤️👨👨❤️💋👨🤍🤎🫶❤️🔥I love your vid’s so much.
Very nice, illustrative yet short. Brevity is the soul of wit, as said. While defining a python function, you should use the term 'parameter', not 'argument'. Am I wrong?
"they're also called required arguments, since their required" i cant…🤣
sys.recursionlimit()
thanks so much
Thank you.
this is python 2
"The function did nothing. Just like we told it to"
"The great thing about computers is that they do EXACTLY what you ask.
The PROBLEM with computers is that they do EXACTLY what you ask them…"
These videos are without a doubt the best programming teaching videos I have ever seen. They are concise, to the point and the monotone voice of this lady is just perfect for this subject matter.
These are so wonderful! I learn and laugh!
Awesome video! Thank you!
you are so scaring socratica madam, why you are not smile.
I am Amir, new for programming, I love your teaching method for shortly and sample, thank you
You people at Socratica are Great.
THANK YOU
I really like the way and style the subjects are approached.
I love you robot lady
I just discovered your channel today. I love the character and the delivery, it's totally clean, logical, and humorous! You make learning Python for a new-be like me totally fun. Wait, it is a character right? You're not really like that, not that there's anything wrong with that. lol! …"you want get an argument from me" That was awesome! lol!
How can I write a function to return the 1st and last characters of a string using define and return HELP, I know I have to index I just don't know how to make the function work.
like the humor and teaching style, however, I wish I could find lessons that show examples that aren't all math. I am a beginner in python and not into math, obviously, and spend more time figuring out the damn math than the programming language.
I love the approach to which the lectures are delivered. It's incredibly well-done. I wish there were updated contents to reflect Python 3 version.
@Socratica – I have been teaching intro. programming for 20 years, and using your videos in class since you started. I often re-watch them to be sure they still reflect my own teaching practices and conventions. Every time I laugh out loud and am astounded at how much practical detail you pack into each short segment. Well done – I'm sure you have inspired and helped hundreds of my students over the years. thank you so much – you are a gem.
Might be the background sound, might be the voice, whatever it is it makes me feel like im playing "Portal" which is epic.
Socratica you're the best!
Socratica you're the best!
Outstanding tutorial and better way to teach computer science topics
amazing tutorial as well as video ….wich Software you use for making these kind of video ❤️
You are amazing ! Thank you ! Thank you ! Thank you!
Oops, I went too far back in time and stumbled into the beginner’s class! I do enjoy the comedy, though. Worth watching for that!
I like this kind of teaching Python very much! Spartan, humorous and elegant: over and over and over and over … hm, again. LOL
When you said interesting but not very helpful I felt that.
To display the dir() you must use print(dir())
This is specially helpful to avoid moments of (Deja Vu x 2)