Python basic knowledge of functions, modules

Source: Internet
Author: User

1.Python module.

Python a. py suffix file, called a module. A python module can contain variables, functions, classes, and variables and functions that can be defined in a class.

2.Python module Import.

(1) All Python module Import mode: From Module name import *

(2) Python module on-demand Import mode: From Module name Import function name/class name

3. function. Common functions are as follows.

STR (o): Converts a number to a string function,

Int (o): Converts a string into a numeric function.

Format (): String formatting function.

Replace: string substitution function.

Input: Enter the function. Code:

#Coding=utf-8defPlus1 (num1,num2): Num=num1+num2returnNumdefMinus1 (num1,num2): Num=num1+num2returnNumclassHe:def __init__(self):Pass    defPlus2 (self,num1,num2): Num=num1+num2returnNumdefMinus2 (self,num1,num2): Num=num1+num2returnNum

Calling code:

Python 3.6.1 (V3.6.1:69C0DB5, Mar, 17:54:52) [MSC v.1900 32bit (Intel)] on Win32type"Copyright","credits" or "license ()"  forMore information.>>> fromHeImport*>>> Num=plus1 (20,15)>>>Print(num)35>>> fromHeImportPlus1>>> Num=plus1 (20,15)>>>Print(num)35>>> fromHeImportHe>>> he=He ()>>> Num=he.plus2 (20,15)>>>Print(num)35>>> num2=25>>>Print("num2:"+str (num2)) num2:25>>> age="2">>>Print(int (age))2>>> str="Uid:{0},uname:{1}">>> Str=str.format ("1","Joyet")>>>Print(str) UID:1, Uname:joyet

>>> str2= "Joyet55"
>>> Str2=str2.replace ("55", "66")
>>> Print (STR2)
>>> a=input ("Please Enter:")
Please enter: 45
>>> Print (a)

Python basic knowledge of functions, modules

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: 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.