Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary - Python

Rating
-
Sold
-
Pages
5
Uploaded on
10-12-2023
Written in
2023/2024

Here you can learn the python basics in just 1hour.

Institution
Course

Content preview

Name: Prithiksha.E
Sub Code: GE3151
Class: B.Tech-1st Year

FUNCTIONS IN PYTHON


Functions

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

Creating a Function

In Python a function is defined using the def keyword:

Example:
def my_function():
print("Hello from a function")
Calling a Function

To call a function, use the function name followed by parenthesis:

Example:
def my_function():
print("Hello from a function")

my_function()
Arguments

Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You can add as
many arguments as you want, just separate them with a comma.

The following example has a function with one argument (fname). When the function is
called, we pass along a first name, which is used inside the function to print the full name:

Example:
def my_function(fname):
print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")
Arguments are often shortened to args in Python documentations.

, Parameters or Arguments?

The terms parameter and argument can be used for the same thing: information that are
passed into a function.

From a function's perspective:

A parameter is the variable listed inside the parentheses in the function definition.

An argument is the value that is sent to the function when it is called.
Number of Arguments

By default, a function must be called with the correct number of arguments. Meaning that if
your function expects 2 arguments, you have to call the function with 2 arguments, not more,
and not less.

Example:
This function expects 2 arguments, and gets 2 arguments:

def my_function(fname, lname):
print(fname + " " + lname)

my_function("Emil", "Refsnes")

If you try to call the function with 1 or 3 arguments, you will get an error:

Example:
This function expects 2 arguments, but gets only 1:

def my_function(fname, lname):
print(fname + " " + lname)

my_function("Emil")


Arbitrary Arguments, *args

If you do not know how many arguments that will be passed into your function, add
a * before the parameter name in the function definition.

This way the function will receive a tuple of arguments, and can access the items
accordingly:

Example:

If the number of arguments is unknown, add a * before the parameter name:

Written for

Course

Document information

Uploaded on
December 10, 2023
Number of pages
5
Written in
2023/2024
Type
SUMMARY

Subjects

$13.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sinkarachselvai

Get to know the seller

Seller avatar
sinkarachselvai Indian Institute of handloom technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions