博客
关于我
python数据分析
阅读量:319 次
发布时间:2019-03-04

本文共 2141 字,大约阅读时间需要 7 分钟。

1.常用数据类型转换

#常用数据类型转换my_str='10'num=int(my_str)print(type(my_str))print(type(num))

2.输入和输出

# 输入和输出str1 = 'hello'str2 = 'world'#输出多个变量时。中间要用,分割# print(str1,str2)# 修改分隔符(默认有一个空格)# print(str1,str2,sep='&')# 查函数功能# ??print# 默认有一个换行符 \n# print('hello',end='')# print('world')# 输入# str1= input('请输入:')# print(str1)# 注意:py3中input返回的都是str,py2是raw_input# 输入多个值str1=input('请输入:').split(',') # 返回一个listprint(str1)

3.格式化输出

# 格式化输出name='wxp'age=18print("我是%s,年龄%d"%(name,age))# format格式化函数print('我叫{},年龄{}'.format(name,age))print('我叫{na},年龄{ag}'.format(na=name,ag=age))print('我叫{0},年龄{1}'.format(name,age))

4.整理桌面文件

import os import shutil# 1.获取桌面路径desktop = os.path.join(os.path.expanduser("C:/Users/AGGRESSIVE2019/"),"Desktop")# print(desktop)# 2.在桌面上创建文件夹name = input('请输入文件夹的名字:')# 3.创建文件夹clean = os.path.join(desktop,name)# 判断路径是否存在isExist = os.path.exists(clean) # True Falseif isExist == False:    os.mkdir(clean)# 3.获取文件name_list = os.listdir(desktop)# print(name_list)# 4.将文件分类for file in name_list:    filepath = os.path.join(desktop,file)    if not os.path.isfile(filepath):        continue    elif os.path.isfile(filepath):        # 分割        fileExpand = os.path.splitext(file)[1]        fileExpand = fileExpand[1:]        expand_file_name = os.path.join(clea,fileExpand)        if not os.path.exists(expand_file_name):            os.mkdir(expand_file_name)        #移动文件        shutil.move(filepath,expand_file_name)

5.查看版本

from sys import version_infoif version_info.major == 2:    print('python2')elif version_info.major == 3:    print('python3')

6.while 和continue

num = 1while num <=5:    if num == 4:        num += 1        continue   # 结束本次循环    print(num)    num += 1num = 1while num <=5:    if num == 4:        num += 1        break   # 结束所有循环    print(num)    num += 1

7.乘法表

# 乘法表i = 1while i<=9: #行数    j = 1    while j<=i: # 行数        print('%d * %d = %d  ' % (j,i, j*i ),end = '')        j +=1    print('')    i +=1

8.综合

# c +=1等价于 c=c+1# 优先级 not>and>orprint('%.2f'%3.14)# while条件num = 1while num<=5:    print(num)    num += 1print('end')# range(1,6)可迭代对象# range(起始数据,结束数据,步长) 步长默认为1,不包含结束数据for value in range(1,6):    print(value)

转载地址:http://elnh.baihongyu.com/

你可能感兴趣的文章
《经济机器是怎样运行的》笔记(三)
查看>>
Python提升回测速度concurrnet.futures模块详解
查看>>
Python语言'类'概念再理解
查看>>
(2019.6.27)Anaconda清华镜像已恢复使用
查看>>
Robomongo使用教程:踩着前辈的路
查看>>
Python中Class类与def函数的区别
查看>>
OpenAI Gym简介及初级实例
查看>>
用Matplotlib和Gym优雅地呈现股票交易智体
查看>>
Github上量化交易相关项目汇总
查看>>
JS取出两个数组中的不同或相同元素
查看>>
Ubuntu 18.04 zip压缩文件及其文件 夹中的所以 内容
查看>>
int 转 CString
查看>>
Edit编辑框自动换行与长度
查看>>
STMF4 : error: #928: incorrect use of va_start
查看>>
如何在Windows上搭建NFS服务器实现开发板与Windows之间的文件共享
查看>>
英语02_单词词性
查看>>
C语言12_预处理 #
查看>>
低通滤波器的设计
查看>>
窄带随机过程的产生
查看>>
随机四则运算
查看>>