Python Base One, pythonbaseone

Source: Internet
Author: User

Python Base One, pythonbaseone

// This is my first day to study python, in order to review, every day I will make notes (2016/7/31)

1. In python, there are using bucket-in funcation. you can use follow to find out hou using built-in funcation:

Dir (_ builtins __)

If you want get BIF detail, you can use follow code:

Help (......) Such as help (input), int ()

 

2. In python, you should be care to indent, if not, it will appear a lots error.

Such as follow, because indent error, it will appear an error:

Temp = input ('input a num you want :')
Guess = int (temp)
If guess = 8:
Print ('you are right ')
Else:
Print ('you are wrong ')

 

3. In python, when you define variable, uppercase and lowercase is different.

Such:

Temp = 'roy'

Temp = 'wyg'

Temp and Temp is not same variable

 

4. In python, if you want to use 'in a string, you can user escape charcters :\

Such:

Print ('Let \'s go! ')

 

5. In python, origin string is userful, maybe the follow result is not you should t

Such:

Str = 'C: \ now \ data'

Print (str)

Result is:

C:

Ow \ data

You can solve this problem by str = r 'C: \ now \ data', this code is equal to str = 'C: \ now \ data'

When you print (str), the result will be c: \ now \ data

 

6. In python, if a string, you want to change row, you can user ''', if you not user ''' and change row, it will appear an error

Such:

Str = ''' this

Is

Me '''

Print (str)

The result is:

This

Is

Me

 

7. In python, import module, such as if you want a rand num, range is 1-10 and type is int, how to achieve it

Import random

Randnum = random. randint (1, 10)

 

// This is second day to study python (2016/8/1)

8. In python, we can see 'E' as 10

Such:

1.5e4 = 15000.0

 

9. In python, type conversion is userful, the follow is base conversion

Float or string-> int

Such:

A = '26' B = int (a) print (B)-> 26

A = 'qq' B = int (a) error

A = 3.14 B = int (a) print (B)-> 3

----------------------------------------

Int or string-> float

Such:

A = '26' B = float (B) print (B)-> 26.0

A = 'qq' B = float (B) error

A = 26 B = float (B) print (B)-& gt; 26.0

------------------------------------------

Int or float-> str

Such:

A = 26 B = str (a) print (B)-> '26'

A = 3.14 B = str (a) print (B)-> '3. 14'

A = 5e19 B = str (a) print (B)-> '5e + 19'

Sometimes, we need to be care to str,

Such:

Str = 'I like apple'

Print (str)-> 'I like apple'

But if it is c = str (5e19), it will appear an error, because str is BIF, we define str again, it will have new means, so it will have error

 

10. In python, we can get the type of variable

Such:

A = 'qq'

Type (a)-> <class 'str' at 0x ----->

A = 3.0

Type (a)-> <class 'float' at 0x --->

A = True

Type (a)-> <class 'bool 'at 0x --->

We recommand follow:

A = 'roy'

Isinstance (a, str)-> True

Isinstance (a, int)-> False

But sometimes you need to know it maybe account an error:

TypeError: isinstance () arg 2 must be a type or tuple of types

The reason is may you define str before, In python, you can change built-in funcation, so when you define variable, you shocould try to avoid user special chararctes. such as try not to use BIF.

 

11. In python, arithmetic operators has +-*/** // %

A + = 4 <=> a = a + 4

A = 2

B = 3

A/B-> 0.6666666666666666
A // B-> 0

B // a-> 1

A % B-> 2

B % a-> 1

A * B-> 8

B ** a-> 9

 

12. In python, logical operators between des and, or, not

Such:

Not Ture

False

 

13. In python, if and else how to use:

Score = int (input ('Please input score :'))

If 90 <= score <= 100:

Print ('A ')

Elif 80 <= score <90:

Print ('B ')

Elif 60 <= score <80:

Print ('C ')

Else:

Print ('D ')

 

14. trinocular operator

Small = x if x <y else y

->

X, y = 4, 5

If x <y:

Small = x

Else:

Small = y

 

15. assert, when condication is false, it will have assertionerror

Such:

Assert 3> 4

>>> Assert 3> 4
Traceback (most recent call last ):
File "<pyshell #1>", line 1, in <module>
Assert 3> 4
AssertionError

 

// Third day study python (2016/8/2)

16. In python, how to achieve 'for 'loop:

For target in expression:

Loop body

Such:

String = 'roy'

For I in string:

Print (I)

->

R

O

Y

Students = ['wyg', 'roy]

For each in students:

Print (each, len (each ))

 

17. In python, range () is often combined with for loop, what's range ()

Range ([start,] stop [, step = 1]) // three para

[] Represent select

Step = 1 represent by default step is 1

So it is from start to stop number list

Range (3)-> range (0, 3)

List (range (4)-> [0, 1, 2, 3]

For I in range (2, 5 ):

Print (I)

Result is: 2 3 4

For I in range (1, 10, 4 ):

Print (I)

Result is: 1 5 9

18. In python, there are also have break and continue

Continue skip current loop

Break end loop

 

19. In python, there are no array, but have list and more power

Such:

MixType = [1, 'roy ', 3.14, [5, 6]

We can also create an empty list:

Empty = []

How to add content to list:

Empty. appen (3)-> 3

Or

Empty. extend ([3, 6, 9])-> 3 3 6 9

Or

Empty. insert (0, 'aaa')-> 3 'aaa 3 6 9

 

 






 

 

 

 

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.