🐍argument & parameter

The difference between argument and parameter

Arguments and parameters are two terms that are often used interchangeably, but they have different meanings in programming. Here are the differences between argument and parameter:

  • Parameter: A parameter is a variable in a function definition that acts as a placeholder for a value that will be passed to the function when it is called. It is a part of the function's signature and defines the type and number of values that the function expects to receive.

  • Argument: An argument is a value that is passed to a function when it is called. It is the actual value that is assigned to the parameter of the function. Arguments are passed to a function in the order that the parameters are defined.

In summary, a parameter is a variable that is defined in a function, while an argument is the value that is passed to that parameter when the function is called.

def gpa(self,student_id:str) -> float:

The function definition "def gpa(self, student_id:str) -> float" means that it is a Python function that takes in two arguments: "self" and "student_id", where "self" refers to the instance of the class and "student_id" is a string argument. The "-> float" part of the definition specifies that the function returns a floating-point number (a decimal number). In other words, the function calculates and returns the GPA (grade point average) of the student with the given ID.

What is the purpose of the "self" parameter in Python class methods

In Python, the "self" parameter is used in class methods to refer to the instance of the class1234. It is always the first parameter in a method definition and is passed implicitly when the method is called on an instance of the class123. The "self" parameter allows access to the attributes and methods of the class within the method123. It is used to differentiate between instance variables and local variables within the method5. The use of "self" makes the code more readable and explicit, as it clearly indicates that the method is being called on an instance of the class

"def display_results(self) -> None:" and "def display_results(self):"

the former specifies the return type of the function, while the latter does not. The "-> None" in the first example indicates that the function does not return anything.

Difference between "list" and "dictionary"

A list[] is an ordered collection of elements, and each element can be accessed using its index. A dictionary{}, on the other hand, is an unordered collection of key-value pairs, and each value can be accessed using its key.

In Python, "print(title[:-1])" means to print the "title" string without its last character.

The square brackets[] are used to slice the string, and the colon: inside the brackets indicates that we want to slice the string. The ":-1" part of the code specifies the range of the slice, where the first colon indicates the start of the slice, and the "-1" indicates the end of the slice. In Python, negative indices are used to count from the end of the string, so "-1" refers to the last character of the string. Therefore, "title[:-1]" returns a new string that contains all the characters of the "title" string except for the last one. Finally, the "print()" function is used to display the resulting string on the console.

By default, the "end" parameter is set to "\n", which means that a newline character is printed at the end of the output. However, by setting the "end" parameter to an empty string, we can print the output without a newline character.

The "{:>12}" part of the code is a string formatting expression that specifies how the spaces should be formatted. The ">" character indicates that the spaces should be right-aligned, while the "12" indicates the width of the field, which is 12 characters. Finally, the empty string "" is used to fill the field with spaces.

Last updated