#34 Python Tutorial for Beginners | Types of Arguments in Python

#34 Python Tutorial for Beginners | Types of Arguments in Python

Introduction to Python Functions

In this section, the speaker introduces the concept of functions in Python and discusses how to pass parameters or arguments to a function.

Defining a Function with Parameters

  • A function is defined using the def keyword followed by the function name and any parameters in parentheses.
  • Parameters are variables that hold values passed into a function when it is called. They can be referred to as formal arguments.
  • To add two numbers, for example, we define a function named sum that takes two parameters named a and b. We then use these parameters to calculate their sum and return or print it.

Types of Arguments

  • There are four types of actual arguments in Python - positional, keyword, default, and variable-length arguments.
  • Positional arguments are passed based on their position or order in the parameter list. If you have 10 variables, you need to make sure that values are assigned to the correct variable based on its position.
  • Keyword arguments are passed with key-value pairs instead of just values. This allows us to pass them out of order since they're identified by their keys rather than their positions.
  • Default arguments have default values assigned so if no value is provided for them during a call they will take on their default value.
  • Variable-length arguments allow us to pass an arbitrary number of positional or keyword arguments.

Conclusion

Functions play an important role in programming languages like Python as they help break down complex tasks into smaller ones that can be reused throughout your codebase. Understanding how to define functions with different types of arguments is essential to writing efficient and effective code.

Function Arguments in Python

In this section, the speaker discusses function arguments in Python and covers three types of arguments: positional, keyword, and variable-length.

Positional Arguments

  • Positional arguments are passed to a function based on their position.
  • The order of the arguments matters when using positional arguments.
  • Example: def some(a, b): where a and b are positional arguments.

Keyword Arguments

  • Keyword arguments are passed to a function with a key-value pair.
  • The order of the arguments does not matter when using keyword arguments.
  • Default values can be set for keyword arguments.
  • If no value is passed for a keyword argument, it will use its default value.
  • Example: def some(age=18, name='Naveen'): where age and name are keyword arguments.

Variable-Length Arguments

  • Variable-length arguments allow you to pass an arbitrary number of values to a function.
  • The syntax for variable-length argument is *args.
  • When using variable-length argument, all additional parameters should be passed as tuples.
  • Example: def add(a, *b): where a is a required parameter and *b allows for multiple optional parameters.

Using Variables and Arguments in Python

In this section, the speaker explains how to use variables and arguments in Python.

Creating a Loop with Variables

  • A loop can be created by adding new values to a variable each time the loop runs.
  • The final value of the variable can then be printed at the end of the loop.
  • An indentation problem may cause an error when running the code.

Customizing Arguments

  • By using a star symbol, any number of values can be passed as an argument.
  • If no value is passed for one of the arguments, it can be set to zero.
  • Different types of arguments, including integers, strings, and floats, can be used together.

Conclusion

The speaker covers different types of arguments in Python, including default, position, keyword, and variable arguments. These concepts are useful for creating loops and customizing functions.

Turn any video into a summary like this

YouTube links, meetings, lectures — with transcripts, search, and chat.

Video description

Check out our courses: AI Powered DevOps with AWS - Live Course :- https://go.telusko.com/AIDevOps-AWS Coupon: TELUSKO10 (10% Discount) Complete Java Developer Course Batch-4: https://go.telusko.com/Complete4 Coupon: TELUSKO10 (10% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) Udemy Courses: Spring: https://go.telusko.com/udemyteluskospring Java:- https://go.telusko.com/udemyteluskojava Java Spring:- https://go.telusko.com/Udemyjavaspring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker For More Queries WhatsApp or Call on : +919008963671 website : https://courses.telusko.com/ Instagram : https://www.instagram.com/navinreddyofficial/ Linkedin : https://in.linkedin.com/in/navinreddy20 TELUSKO Android App : https://bit.ly/TeluskoApp Discord : https://discord.gg/D8hWe9BqfF In this lecture, we are discussing types of arguments in python : -- formal argument -- actual argument Actual arguments have 4 types: 1)position 2)keyword 3)default 4)variable length argument Position argument: -- During a function call, values passed through arguments should be in the order of parameters in the function definition. This is called positional arguments. e.g def person(name,age): print(name) print(age) add(5,6) keyword argument: -- During a function call, values passed through arguments don’t need to be in the order of parameters in the function definition. Keyword arguments can achieve this. -- All the keyword arguments should match the parameters in the function definition. e.g person(age=28,name='navin') default argument: -- Default arguments are values that are provided while defining functions. -- The assignment operator = is used to assign a default value to the argument. -- Default arguments become optional during the function calls. -- If we provide a value to the default arguments during function calls, it overrides the default value. -- The function can have any number of default arguments. -- Default arguments should follow non-default arguments. e.g def person(name,age=28): print(name) print(age) person('navin') variable length argument: -- if you want to pass multiple value in a function call we can use variable length argument def sum(a,*b): print(a) print(b) c=a for i in b: c=c+i print(c) sum(5,6,34,78) Github :- https://github.com/navinreddy20/Python- Python for Beginners :- http://bit.ly/3JOLQhl Java and Spring Framework For beginners with Spring Boot : - http://bit.ly/3LDMj8D Java Tutorial for Beginners (2023) :- http://bit.ly/3yARVbN Subscribe to our other channel: Navin Reddy : https://www.youtube.com/channel/UCxmk... Telusko Hindi : https://www.youtube.com/channel/UCitz... Donation: PayPal Id : navinreddy20 Patreon : navinreddy20 http://www.telusko.com/contactus