The example explains how to call and define functions in Python, and the example explains python

Source: Internet
Author: User

The example explains how to call and define functions in Python, and the example explains python

Call the function:

#! /Usr/bin/env python3 #-*-coding: UTF-8-*-# function call >>> abs (100) 100 >>> abs (-110) 110 >>> abs (12.34) 12.34 >>> abs (1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: abs () takes exactly one argument (2 given)> abs ('A') Traceback (most recent call last ): file "<stdin>", line 1, in <module> TypeError: bad operand type for abs (): 'str' >>> max (1, 2) 2 >>> max (2, 3, 1,-5) 3 >>> int ('000000') 123 >>> int (123) 12 >>> str (1.23) '1. 23' >>> str (100) '20140901' >>> bool (1) True >>> bool ('') false >>> a = abs # variable a points to the abs function, which is equivalent to referencing >>> a (-1) # therefore, you can call abs function 1> n1 = 255> n2 = 1000> print (hex (n1 )) 0xff >>> print (hex (n2) 0x3e8

Define functions:

#! /Usr/bin/env python3 #-*-coding: UTF-8-*-# Function Definition def myAbs (x): if x> = 0: return x else: return-x a = 10 myAbs (a) def nop (): # null Function pass

The pass statement does not do anything.
In fact, pass can be used as a placeholder. For example, if you haven't thought about how to write the function code, you can first write a pass to run the code.

If age> = 18: pass # The Code has a syntax error if pass # Is Missing >>> if age> = 18 :... file "<stdin>", line 2 ^ IndentationError: expected an indented block >>> myAbs (1, 2) Traceback (most recent call last): File "<stdin> ", line 1, in <module> TypeError: myAbs () takes 1 positional argument but 2 were given> myAbs ('A') Traceback (most recent call last ): file "<stdin>", line 1, in <module> File "<stdin>", line 2, in myAbs TypeError: unorderable types: str ()> = int () >>> abs ('A') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bad operand type for abs (): 'str' def myAbs (x): if not isinstance (x, (int, float): raise TypeError ('bad operand type ') if x> = 0: return x else: return-x> myAbs ('A') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in myAbs TypeError: bad operand type


Return two values?

import math def move(x, y, step, angle = 0):  nx = x + step * math.cos(angle)  ny = y - step * math.sin(angle)  return nx, ny  >>> x, y = move(100, 100, 60, math.pi / 6) >>> print(x, y) 151.96152422706632 70.0 


In fact, the above is just an illusion that the Python function returns a single value.

>>> r = move(100, 100, 60, math.pi / 6) >>> print(r) (151.96152422706632, 70.0) 

Actually, a tuple is returned!
However, in syntax, a tuple can be returned without parentheses, while multiple variables can accept a tuple at the same time and assign the corresponding value by position.
Therefore, the Python function returns a tuple, but it is easier to write.
If no return statement is returned after the function is executed, the return None statement is automatically returned.

Exercise:

import math def quadratic(a, b, c):  x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)  x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)  return x1, x2  x1, x2 = quadratic(2, 5, 1) print(x1, x2)  >>> import math >>> def quadratic(a, b, c): ...  x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a) ...  x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a) ...  return x1, x2 ... >>> x1, x2 = quadratic(2, 5, 1) >>> print(x1, x2) -0.21922359359558485 -2.2807764064044154

Articles you may be interested in:
  • In-depth introduction to the use of parameters in Python functions and the traps of default parameters
  • Use Python built-in modules and functions to convert numbers in different hexadecimal formats
  • Usage Analysis of element traversal using the enumerate function in python
  • Exploring the use of open functions in python
  • Sort out common mathematical functions in the math module of Python
  • Python nested functions use external function variables (Python2 and Python3)
  • Use * args and ** kwargs in Python functions to pass variable length parameters
  • Analysis of Function Definition instances in python Development
  • Compile functions in the Python Django framework
  • Wrap view functions in the Python Django framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.