Python Basic Concepts 2

Source: Internet
Author: User

1.bool Variables and operations

Print 1 + 1 = = 2
Print 1 + 1! = 2

Print 1 + 1 = = 2 and 1 + 1 = = 3
Print 1 + 1 = = 2 or 1 + 1 = = 3

Print not 1 + 1 = = 2

print 1 in [1, 2, 3] Output:True
False
False
True
False
True 2.ifLight = "Red"
if light = = "Red":
print "stop!"
elif Light = = "Green":
print "go!"
elif Light = = "Yellow":
print "Slow down!" 3.whileA =While a<100:a = a +1Print a 4. for in
Contact = {"Li Lei": "1303030", "Hanmeimei": "1450303"}for i in Contact:    print I, Contact[i]
Output:
"Li Lei": "1303030"
"Hanmeimei": "1450303"
5.break Continuethe meaning is the same as most other languages 6. Custom Functionsdef addnum (num):
A = 0
For i in Num:
A = a + I
return a

num = [1, 3, 4, 5, 9]

print addnum (num) 7. Function Scopevery similar to C + + x = 0
y = 0
def changex ():
Global x//global can use variables outside the function inside a function
x = Ten
y = ten
Print x
Print y
Changex ()
Print x
Print y  8.importImport Math//Importing package

Print Math.PI

Print dir (math)//dir can view all the methods of a package 

Python Basic Concepts 2

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.