Python Basics---simple syntax

Source: Internet
Author: User

1.if statements

>> A = 10
>> B = 20
>> if (a>b):
Print ("a max!")
Else
Print ("b max!")

b max!

>> student = "Zhangxiaoyu"
>> if (student = = "Zhanxgiaoyu"):
Print ("YES")
Else
Print ("NO")

NO

>> if (Student! = "Zhanxgiaoyu"):
Print ("YES")
Else
Print ("NO")

YES
"In and not" represents the contained relationship "

>> hi = "Hello python"
>> if ("H" in hi):
Print ("Yes")
Else
Print ("No")

Yes

>> if ("" in hi):
Print ("Yes")
Else
Print ("No")

Yes

>> if ("I" in hi):
Print ("Yes")
Else
Print ("No")

No

"If statement can be Boolean type judged"

>> A = True
>> If a:
Print ("A is True")
Else
Print ("A is no True")

A is True

2.for statements

>> names = ["Xiaoyu", "Xiaowen", "Huahua", "Qiongge"]
>> for name in names:
Print (name)

Xiaoyu
Xiaowen
Huahua
Qiongge

>> for name in names:
Print (name)
if (name = = "Huahua"):
Print ("Yes")
Break

Xiaoyu
Xiaowen
Huahua
Yes

>> for name in names:
Print (name)
if (name = = "Huahua"):
Print ("Yes")
Continue

Xiaoyu
Xiaowen
Huahua
Yes
Qiongge

Range () function: zero-based looping by default, or you can set the start position and step size

>> for I in range (10):
Print (i)

01
2
3
4
5
6
7
8
9

>> for I in Range (1,20,5):
Print (i)

1
6
11
16
3. Arrays and dictionaries
(1) array

>> lists = [1,2,3,4,5,6, ' a ']
>> Lists[0]
1
>> Lists[6]
A
>> lists[6] = ' n '
>> Lists[6]
N
>> lists.append ("V")
>> lists
[1, 2, 3, 4, 5, 6, ' n ', ' V ']
(2) Dictionary
>> ret = {"Usenames": "Xiaoyu", "Password": "12345"}
>> Ret.keys ()
Dict_keys ([' Password ', ' usenames '])
>> ret.values ()
Dict_values ([' 12345 ', ' Xiaoyu '])
>> Ret.items ()
Dict_items ([' Password ', ' 12345 '), (' Usenames ', ' Xiaoyu ')])
4. Functions, classes, and methods
(1) function
def keyword to define the function.
>> def add (A, B):
Print (A+B)

>> add
3
>> sum = add
3
>> def add (A, B):
Return (A+B)
>> Add (1,9)
10

If you do not want to pass arguments when calling the Add () function, you can set the default parameters.

>> def add (a = 1,b = 9):
Return (A+B)
>> Add ()
10
>> Add (10,20)
30
(2) Classes and methods
Class A (object):
def add (self,a,b):
Return (A+B)

Count = A ()
Print (Count.add (3,9))

Class A ():
def Init(self,a,b): #初始化
SELF.A = Int (a)
self.b = Int (b)

def add (self):
Return self.a+self.b

Count = A (0,0)
Print (Count.add ())
Class A (): #B继承A
def add (self, A, b):
Return (A+B)

Class B (A):
def sub (self,a,b):
Return (A-B)

Count = B ()
Print (Count.add (10,90))
Print (Count.sub (10-9,9))
5. Module (class library, module)
(1) There is a CTime () method below the time module to get the current
Import time
Print (Time.ctime ())

From time import CTime #只会用到tiem下的ctime () method
Print (CTime ())

Import time
Print (Time.ctime ())
Print (Time.sleep (2))
Print ("Hibernate two Seconds")
Print (Time.sleep (2))
Print (Time.ctime ())

From time import # "" to represent all methods below the module
Print (CTime ())
Print ("Hibernate 3 Seconds")
Print (Sleep (3))
Print (CTime ())
5. Exceptions
(1)
Try
Open ("Abc.txt", ' R ')
Except Filenotfounderror: #试图打开一个不存在的文件与目录
Print ("an exception!") ")
(2)
Try
Print (AA)
Except Nameerror: #使用一个还未赋值对象的变量
Print ("This is a name exception")
(3)
Try
Open ("Abc.txt", ' R ')
Print (AA)
Except Exception (baseexception) as msg:
Print ("This is an exception")
Print (msg)
[Errno 2] No such file or directory: ' Abc.txt '
All exception classes in Python inherit exception, so you can use it to receive all types of exceptions. Since the python2.5 version, all exceptions have a new base class of Baseexception. Exception also inherits from Baseexception.
(4) Exception and if statement mates
Try
AA = "Python"
Print (AA)
Except Exception as msg:
Print ("With exception exists")
Else
Print ("Normal output, no exception")
Try
AA = "Python"
Print (AA)
Except Exception as Meg:
Print (msg)
Finally
Print ("executes" whether or not an exception exists)

Python Basics---simple syntax

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.