python一

时间:2023-03-09 05:41:33
python一

一安装工具 pip easy_install

 import os
print (os.getcwd())
1. 自带package和外部package
     1.1 自带package举例: os; os.getwd()
2. 外部package以及管理系统介绍: easy_install, pip (comes with Python 3.4)
3. 环境变量中配置easy_install, pip  
pip install ***安装工具
4. 使用easy_install, pip安装package举例
可以抓取网页
>>> import requests
>>> r = requests.get('https://api.github.com/events')
>>> r.text
>>> r.url
>>> r.encoding
二 数据类型
1. 字符串:
          一串字符
          显示或者打印出来文字信息
          导出
          编码:# -*- coding: utf-8 -*-
          单引号,双引号,三引号 三引号是打印多行
          不可变(immutable)
          Format字符串
               age = 3
               name = "Tom"
               print("{0} was {1} years old".format(name, age))
          联合:+: print(name + " was " + str(age) + " years old")
          换行符: print("What's your name? \nTom") 
2. 字面常量(literal constant):
可以直接以字面的意义使用它们:
如:6,2.24,3.45e-3, "This is a string"
常量:不会被改变
3. 变量:
          储存信息
          属于identifier
          identifier命名规则:
               第一个字符必须是字母或者下划线
          其余字符可以是字母,数字,或者下划线
          区分大小写
          如:合法:i, name_3_4, big_bang
               不合法:2people, this is tom, my-name, >123b_c2

4. 注释: #  pycharm 是ctrl + /

5. 缩进(Indentation)

2.2 数据类型2: Numeric & String

1. Python数据类型
     1.1 总体:numerics, sequences, mappings, classes, instances, and exceptions
     1.2 Numeric Types: int (包含boolean), float, complex
     1.3 int: unlimited length; float: 实现用double in C, 可查看 sys.float_info; complex: real(实部) & imaginary(虚部),用z.real 和 z.imag来取两部分
     1.4 具体运算以及法则参见:https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
     1.5 例子
import sys
a = 3            动态语言不需要指定类型
b = 4
c = 5.66
d = 8.0
e = complex(c, d)    复数
f = complex(float(a), float(b))
print ("a is type" , type(a))
print ("b is type" , type(b))
print ("c is type" , type(c))
print ("d is type" , type(d))
print ("e is type" , type(e))
print ("f is type" , type(f))
print(a + b)
print(d / c)       
print (b / a)
print (b // a)            两个除号是取整
print (e)
print (e + f)
print ("e's real part is: " , e.real)
print ("e's imaginary part is: " , e.imag)
print (sys.float_info)
3 中文设置
1. print中的编码:
        编码:# -*- coding: utf-8 -*-
2. print中的换行
          print("What's your name? \nTom")
4 . List
     创建
     访问
     更新    
     删除
     脚本操作符
     函数方法
Code:
# -*- coding: utf-8 -*-
#创建一个列表
number_list = [1, 3, 5, 7, 9]     用方括号表示的是list 列表   里面的元素类型可以不一致 ,这和C的数组不一样,但是index是从0开始
string_list = ["abc", "bbc", "python"]
mixed_list = ['python', 'java', 3, 12]       
#访问列表中的值
second_num = number_list[1]  
third_string = string_list[2]
fourth_mix = mixed_list[3]
print("second_num: {0} third_string: {1} fourth_mix: {2}".format(second_num, third_string, fourth_mix))
#更新列表
print("number_list before: " + str(number_list))
number_list[1] = 30
print("number_list after: " + str(number_list))
#删除列表元素
print("mixed_list before delete: " + str(mixed_list))
del mixed_list[2]   可以直接删除 del
print("mixed_list after delete: " + str(mixed_list))
#Python脚本语言
print(len([1,2,3])) #长度            
print([1,2,3] + [4,5,6]) #组合
print(['Hello'] * 4) #重复  
print(3 in [1,2,3]) #某元素是否在列表中 
#列表的截取
abcd_list =['a', 'b', 'c', 'd'] 
print(abcd_list[1])
print(abcd_list[-2])  #从后面数第二个
print(abcd_list[1:])  #从第二个开始后所有的数据
# 列表操作包含以下函数:
# 1、cmp(list1, list2):比较两个列表的元素 
# 2、len(list):列表元素个数 
# 3、max(list):返回列表元素最大值 
# 4、min(list):返回列表元素最小值 
# 5、list(seq):将元组转换为列表 
# 列表操作包含以下方法:
# 1、list.append(obj):在列表末尾添加新的对象
# 2、list.count(obj):统计某个元素在列表中出现的次数
# 3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
# 4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
# 5、list.insert(index, obj):将对象插入列表
# 6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
# 7、list.remove(obj):移除列表中某个值的第一个匹配项
# 8、list.reverse():反向列表中元素
# 9、list.sort([func]):对原列表进行排序