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

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

常用数据类型转换

my_str = '10'
num = int(my_str)
print(type(my_str))
print(type(num))

输入和输出

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

格式化输出

name = 'wxp'
age = 18
# 使用format格式化函数
print('我叫{},年龄{}'.format(name, age))
# 使用format指定位置
print('我叫{0},年龄{1}'.format(name, age))
# 使用format命名参数
print('我叫{na},年龄{ag}'.format(na=name, ag=age))

整理桌面文件

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

查看版本

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

while 和 continue

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

乘法表

i = 1
while i <= 9:
j = 1
while j <= i:
print('%d * %d = %d ' % (j, i, j * i), end='')
j += 1
print('')
i += 1

综合

# c +=1 等价于 c = c + 1
# not > and > or
print('%.2f' % 3.14)
# while 条件
num = 1
while num <= 5:
print(num)
num += 1
print('end')
# range(1,6)可迭代对象
for value in range(1, 6):
print(value)

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

你可能感兴趣的文章
OpenCV中的监督学习
查看>>
opencv中读写视频
查看>>
OpenCV中遇到Microsoft C++ 异常 cv::Exception
查看>>
opencv之cv2.findContours和drawContours(python)
查看>>
opencv之namedWindow,imshow出现两个窗口
查看>>
opencv之模糊处理
查看>>
Opencv介绍及opencv3.0在 vs2010上的配置
查看>>
OpenCV使用霍夫变换检测图像中的形状
查看>>
opencv保存图片路径包含中文乱码解决方案
查看>>
OpenCV保证输入图像为三通道
查看>>
OpenCV入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
opencv图像分割2-GMM
查看>>
opencv图像分割3-分水岭方法
查看>>
opencv图像切割1-KMeans方法
查看>>
OpenCV图像处理篇之阈值操作函数
查看>>
opencv图像特征融合-seamlessClone
查看>>
OpenCV图像的深浅拷贝
查看>>
OpenCV在Google Colboratory中不起作用
查看>>
OpenCV学习(13) 细化算法(1)(转)
查看>>
OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
查看>>