Python编程入门学习笔记(二)

Python编程入门学习笔记(二)

### 变量:代表某个值的名称

### 语法糖


```python
a = 10
b = 20

a,b = b,a
print("a is {},b is {}".format(a,b))
```

    a is 10,b is 20
    

### 命名规范  
1、标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线。  
2、标识符名称的其他部分可以由字母(大写或小写)、下划线(‘_’)或数字(0-9)组成。


```python
#大小写敏感
n = 10
N = 10
```


```python
#取小数位数的函数
round(100/3,3)
```




    33.333



### 代码规范建议
- 1、不要使用单字符。  
- 2、变量名能清晰表达变量的意思。  
- 3、合理使用字母中间下划线。

### 变量类型
1、字符串 str  
2、数字 int float complex ...  
3、列表 list  
4、元祖 tuple  
5、字典 dict  

### 数值类型


```python
number = 10
number = number + 10
number += 10
```


```python
number -= 10
number *= 10
number /= 10
```


```python
import math
```


```python
# 乘方、开方
math.pow(3,10)
```




    59049.0




```python
#推荐的写法
3 ** 10
```




    59049




```python
#向下取整
math.floor(2.23242)
```




    2




```python
#向上取整
math.ceil(2.234234)
```




    3




```python
#度数的转换
math.radians(180)
```




    3.141592653589793




```python
#正弦
math.sin(math.pi/2)
```




    1.0




```python
#最小值
min(10, 12, 234, 100, 1)
```




    1




```python
#最大值
max(10, 12, 234, 100, 1)
```




    234




```python
#求和
sum([10, 12, 234, 100, 1])
```




    357




```python
#求商和余数
divmod(10 , 3)
```




    (3, 1)



### bool型


```python
#true可以代表1,false可以代表0
True,False
```




    (True, False)




```python
True == 1
```




    True




```python
False == 0
```




    True




```python
#不推荐这样使用,没有意义,有一个特例,后面会讲到
True + 10
```




    11




```python
100 > 10
```




    True



### bool类型,运算:与运算、或运算、非运算


```python
#与运算,同位真则为真
True and True
```




    True




```python
True and False
```




    False




```python
#或运算,只要一个为真则为真
True or False
```




    True




```python
#非运算,取反操作
not True
```




    False



| 操作符 |  解释 |
| :-| -:| 
|  <   | 小于 |
|  <=   | 小于等于 |
|  >   | 大于 |
|  >=  | 大于等于 |
|  ==  | 等于 |
|  !=  | 不等于 |
|  is  | 是相同对象 |

### 字符串


```python
#字符串可以用双引号,也可以用单引号,通过单双引号的恰当使用,可以避免不必要的字符释义(escape),也就是说可以避免使用"\" (转义字符)
line = "hello world"
line = "hello world\""
line = 'hello \'world'
line
```




    "hello 'world"




```python
#字符串的加法操作
line_1 = "hello ,"
line_2 = "world !"
line_1 + line_2
```




    'hello ,world !'




```python
#字符串的乘法操作
line = "hello "
line * 10
```




    'hello hello hello hello hello hello hello hello hello hello '




```python

#返回字符串的长度
len(line)
```




    6




```python
#字符串是不可变类型的变量
line = 'hello'
line_copy = line
#id函数,返回一个身份识别符,可以理解为一个变量的内存地址
id(line),id(line_copy)
```




    (2512609178880, 2512609178880)




```python
line = "world"
id(line),id(line_copy)
```




    (2512608573120, 2512609178880)



猜你喜欢

转载自blog.csdn.net/sonicgyq_gyq/article/details/80606273