Python study notes (b): The Basics

Chapter III Basics

variable

Variable can not start with a number;
letter case is different;
= is the assignment of meaning, is the name of the left, the right is the value, not write reversed.

String

>>> 5+8  #数字相加
13
>>> '5'+'8'  #数字两边加上引号就变成字符串拼接
'58'
>>> 'python i love you"  #不可以一边单引号一边双引号
SyntaxError: EOL while scanning string literal

字符串内容需要单引号怎么办?
>>> 'let's go'  	#错误
SyntaxError: invalid syntax
>>> "let's go"		#用不同的引号
"let's go"
>>> 'let\'s go' 		#转义字符
"let's go"

The original string

字符串内容带有反斜杠时
>>> string = 'C:\now'
>>> string
'C:\now'
>>> print(string)
C:
ow
可以用反斜杠对反斜杠转义:
>>> string = 'C:\\now'  
>>> print(string)
C:\now
使用原始字符串,加英文字母r即可:
>>> string = r'C:\now'
>>> print(string)
C:\now

Whether or not the original string, can not end with a backslash as, or that the string is not finished, the next line of meaning.

>>> string = 'C:\now\'
SyntaxError: EOL while scanning string literal
若是非要加反斜杠在字符串末尾:
>>> str = r'C:\Program Files\FishC\Good'+'\\'
>>> print(str)
C:\Program Files\FishC\Good\


Long strings

Very large number of rows, with triple quoted string ( "" "content" "")
of the conditional branch
<, <=,>,> =, ==,! =

if conditions:
the conditions for true operation performed
else:
the condition is false operations performed

Note: if and else behind thecolonIt can not be omitted.

while loop

while conditions: # attention colon
condition is true execution of the operation

and operator

>>> (3>2) and (1<3)
True

The introduction of foreign aid

random module in the randint () returns a random integer.

>>>secret = random.randint(1,10)

type of data

1. Integer

>>> 520+1413
1933

2. Float
E notation is scientific notation, represent a particularly large or very small numbers

>>> a = 0.0000000000000000000000000065
>>> a
6.5e-27

3. Boolean
True is equivalent to 1, False not do the equivalent of 0,0 divisor

>>> True + True
2
>>> True + False
1
>>> True/False
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    True/False
ZeroDivisionError: division by zero

4. The type conversion
function: int (), float () , str ()

>>> a = '520'
>>> b = int(a)
>>> a,b
('520', 520)

>>> c = 5.99
>>> d = int(c)
>>> c,d
(5.99, 5)

Note: converting floating point to integer type, python will do "truncating" direct cut after the decimal point.

Obtain information about the type of

type () function
the isinstance (data to be determined, the specified data type), the function returns a Boolean value.

Published 11 original articles · won praise 0 · Views 69

Guess you like

Origin blog.csdn.net/qq_43863790/article/details/104069204