python 快速入门(一)

一. 安装

下载python安装包,https://www.python.org/downloads/

二. 变量和简单数据类型

####1.变量

message = "Hello Python world!"
print(message)  #Hello Python world!"

####2.变量命名规则

  • 包含字母、数字和下划线,注意!!开头不能为数字
  • 无空格
  • 不能与关键字重复,如 print
  • 慎用小写字母l和大写字母O,容易跟数字1和0混淆

####3.常量
所谓常量就是不能变的变量,比如常用的数学常数π就是一个常量。在Python中,通常用全部大写的变量名表示常量

PI = 3.14159265359

####4. 字符串
######字符串简单大小写转换

name = "ada lovelace"
print(name.title()) #Ada Lovelace    首字母大写
print(name.upper()) # ADA LOVELACE    全部字母大写
print(name.lower()) # ada lovelace    全部字母小写

######字符串合并

first_name = "ada"
lase_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)   #ada lovelace

######单引号’'和双引号"" 问题
######多行输入

print('''line1
line2
line3''')

####5.整数
Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080,0,等等。

计算机由于使用二进制,所以,有时候用十六进制表示整数比较方便,十六进制用0x前缀和0-9,a-f表示,例如:0xff00,0xa5b4c3d2,等等

####6.浮点数
浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的,比如,1.23x109和12.3x108是相等的。浮点数可以用数学写法,如1.23,3.14,-9.01,等等。但是对于很大或很小的浮点数,就必须用科学计数法表示,把10用e替代,1.23x109就是1.23e9,或者12.3e8,0.000012可以写成1.2e-5,等等。

整数和浮点数在计算机内部存储的方式是不同的,整数运算永远是精确的(除法难道也是精确的?是的!),而浮点数运算则可能会有四舍五入的误差。

####7.布尔值 (and、or、not运算)

print(True and True)          #True
print(True and False)         #False
print(False and False)        #False

print(True or True)              #True
print(True or False)             #True
print(False or False)            #False

print(not True)             #False
print(not False)              #True

####8.空值 None

三. 字符串编码

需要在python文件开头加上两行代码,不然中文输出可能会有乱码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

四. 列表和元组

在python中是一种有序集合,相当于JavaScript中的数组

classmate = ["xiaoming","xiaofang","xiaohong"]

####1.访问列表元素,索引0开始

#正数
print(classmate[0]) #xiaoming
print(classmate[1]) #xiaofang
print(classmate[2]) #xiaohong
print(classmate[3]) 
#Traceback (most recent call last):
#File "F:/neng/python/name.py", line 5, in <module>
#    print(classmate[3])
#IndexError: list index out of range

#倒数
print(classmate[-1]) #xiaohong
print(classmate[-2]) #xiaofang
print(classmate[-3]) #xiaoming
print(classmate[-4]) 
#Traceback (most recent call last):
#File "F:/neng/python/name.py", line 5, in <module>
#    print(classmate[-4])
#IndexError: list index out of range

####2.列表长度 len()

print(len(classmate)) #3

####3.增删改
a.增:append,在列表最后插入;insert,选择位置插入
b.删:pop,默认弹出末尾元素,也可选择弹出元素(有返回值);del ,删除列表索引位置元素;remove,删除首个符合条件的元素(并不是删除特定的索引)
c.改:直接赋值

classmate = ["a","b","c","d","e","f","g","h"]
#增
classmate.append("f")
print(classmate)  #['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'f']
classmate.insert(2,"www")
print(classmate)  #['a', 'b', 'www', 'c', 'd', 'e', 'f', 'g', 'h', 'f']
#删
classmate.pop()
print(classmate)  #['a', 'b', 'www', 'c', 'd', 'e', 'f', 'g', 'h']
classmate.pop(2)
print(classmate)  #['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
del classmate[3]
print(classmate)  #['a', 'b', 'c', 'e', 'f', 'g', 'h']
del classmate[3]
print(classmate)  #['a', 'b', 'c', 'f', 'g', 'h']
classmate.remove("b")
print(classmate)  #['a', 'c', 'f', 'g', 'h']
#改
classmate[3] = "hello"
print(classmate)  #['a', 'c', 'f', 'hello', 'h']

####4.操作列表 range,for … in ,min , max ,sum

#创建数字列表
lists = range(1,5) 
print(list) #[1,2,3,4]
#循环
for key in lists:
     print(key)
#1
#2
#3
#4
#5
print(max(list)) #4
print(min(list)) #1
print(sum(list)) #10
#列表解析
squares = [value**2 for value in range(1,11)]
print(squares) #[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

####5.列表切片 [:]

my_foods = ['pizza', 'falafel', 'carrot cake', 'rice', 'cannoli']
print(my_foods[1:2]) #['falafel']
print(my_foods[:2])  #['pizza', 'falafel']
print(my_foods[1:])  #['falafel', 'carrot cake', 'rice', 'cannoli']
print(my_foods[-2:])  #['rice', 'cannoli']
print(my_foods)  #['pizza', 'falafel', 'carrot cake', 'rice', 'cannoli']

####6.判断值是否在列表中 或者不在列表中

my_foods = ['pizza', 'falafel', 'carrot cake', 'rice', 'cannoli']
print('haha' not in my_foods) #True
print('haha' in my_foods) #False

####7.元组 tuple
一旦初始化就不能修改

 classmates = ('Michael', 'Bob', 'Tracy')

五. 条件判断if

if <条件判断1>:
    <执行1>
elif <条件判断2>:
    <执行2>
elif <条件判断3>:
    <执行3>
else:
    <执行4>

猜你喜欢

转载自blog.csdn.net/weixin_38407447/article/details/84426809