Python Basics (2)

Source: Internet
Author: User
Tags set set python list


The sequence is the most basic data structure, each element in the sequence is assigned a number, its position index first is 0, the second is 1, and so on
The list is the most commonly used Python data type, which appears as a comma-separated value within a square bracket
Data items for a list do not need to have the same type

Python list functions

ordinal function
1 len (list)
Number of list elements
2 Max (list)
Returns the maximum value of a list element
3 min (list)
Returns the minimum value of a list element
4 list (SEQ)
Convert a tuple to a list


Tuples and lists are similar, except that elements of tuples cannot be modified
Tuples use parentheses, the list uses square brackets
Tuple creation is simple, just add elements in parentheses and separate them with commas.

Set Set

A collection is a sequence of unordered, non-repeating elements.
The basic function is to test the membership and remove duplicate elements

Li = [] Creates a list, which is actually called the list () method inside Python.
List () Another way to create lists
Note: The class is appended with a parenthesis (), which is called the _init_ method of the class, which is required to receive a parameter when executing this method.
In list (11,22,33,44), for example, a tuple is passed in.
Inside the _init_, a for loop is actually executed, and the For loop goes through all the elements in the tuple and then goes to create it.

DIC = {"K1": v123} Creates a dictionary, Key:value is a key-value pair, and a key-value pair represents an element of the dictionary
SE = {"123", "456"} Creates a collection, and creating a dictionary is much like creating with curly braces {}, but there are different places, no longer using key-value pairs as an element, but writing something that is an element.


Process-oriented coding:
Meaning is written from top to bottom, according to the logic 1.1 point of writing, when encountering the same function, copy and paste the same code implementation, this is called process-oriented programming.
Poor code readability
Low execution efficiency

Functional Programming:
Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called
Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."
Functional programming The most important thing is to enhance the reusability and readability of code

Defining functions

Name is called the formal parameter of function func, abbreviation: Parameter
def func (name):
Print Name

Execute function
' Wupeiqi ' is called the actual argument of function Func, referred to as: argument
Func (' Wupeiqi ')

function definition:
def function name (): The first step to create a function
The function body is put into memory, the function body is not executed when it is created, and is executed when the call is made.
Call function ()
Return value The second step, once the function executes a return, the function execution procedure terminates immediately, no longer executes downward.

# General Parameters: in strict accordance with the smooth, the actual parameters are assigned to the formal parameters

# def send (name):
# ...
# Send ("Eric")

def sendmail (mail_addr): #第一步, create function (MAIL_ADDR is formal parameter) step three, mail_addr = {str} ' [email protected] '
Try: # This function is to catch the exception, not yet learned.
Import Smtplib
From Email.mime.text import Mimetext
From email.utils import formataddr

msg = mimetext (' message content ', ' plain ', ' utf-8 ')
msg[' from ' = FORMATADDR (["Fukuda", ' [email protected] ')
msg[' to ' = Formataddr (["Leave", ' [email protected] ')
msg[' Subject ' = "Subject"

Server = Smtplib. SMTP ("", 25)
Server.login ("[Email protected]", "521why,.")
Server.sendmail (' [email protected] ', [Mail_addr,], msg.as_string ()) #将收件人地址 ' [email protected] ' change to a variable
Server.quit ()
Except: #只要try下面的代码, catch an exception, execute the code inside the except
# return False #以上代码执行失败, returns a false
Return "No" #返回的也可以是字符串
# return True #否则, returns a true
Return "Yes" #return返回值给函数调用者sendmail,

# SendMail ()
# ret = sendmail (' [email protected] ') #第二步, call function (' [email protected] ' is the actual parameter)
# ret = SendMail ("[email protected]")
# Print (ret)
# if Ret = = True: #通过判断ret是False还是True来显示发送状态
# # if Ret = = "CC": #也可以判断返回的是不是定义的字符串值
# print ("Send Success")
# Else:
# print ("Send Failed")

While True:
EM = input ("Please enter email address:")
# SendMail (EM)
result = SendMail (EM) #em是参数传递的内容, can be multiple
# Print (Result)
if result = = "Yes":
Print ("Send Success")
Print ("Send Failed")

Python Basics (2)

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.