python简单笔记
Remarks:python中注意缩进(Tab键或者4个空格)
print(输出)
格式:print(values)
字符串、数字、变量等都可以输出:
实例:
print(1)->1
print(1+1)->2
a = "hello"
print(a)->hello
print(f"a的值是{a}")->a的值是hello
多行输出:
print("""aaaaaaaaaaaa
aaaaaaaaa
aaaaaaaaaa""")
结果:
aaaaaaaaaaaa
aaaaaaaaa
aaaaaaaaaa
说明括号中 f 和 {变量} 配合可提取字符串中的变量,同print("a的值是",a)效果一样
f 和 {变量} 也可在变量中使用
>>> a = "hello"
>>> b = f"我后面将会显示a的值 {a} "
>>> print(b)
我后面将会显示a的值 hello
换行输出
实例:
>>> print("ABC\nDEF")
ABC
DEF
不换行输出
格式:end=‘’
实例:
a = "ABC"
b = "DEF"
print(a,end='')
print(b)
执行结果:
ABCDEF
变量
变量
格式:变量名称 = values
实例:
one = 1
two = 2
three = one + two
print(three)
输出结果:
3
全局变量
全局可使用
你可以这样写:
var = 520
def fun():
var = 1314
print(var, end='')
fun()
print(var)
执行结果:
1314520
也可以这样写使用 global 关键字:
def fun():
global var
var = 1314
print(var, end='')
fun()
print(var)
执行结果:
13141314
一般多用在函数内,声明变量的作用域为全局作用域。
下面是一个错误的示例:
def fun():
var = 1314
print(var, end='')
fun()
print(var) # 这一步就会报错因为var为函数中的局部变量,外面根本没用var这个变量
注意: 尽量不要使用全局变量,会导致代码可读性变差,代码安全性降低
格式化
format
格式 {位置0}{位置1}.format(参数a,参数b)
注意:format前面有个点.
实例1:
>>> a = "one"
>>> b = "two"
>>> print("{1}比{0}大".format(a,b)) #{}中取第一个值位置参数就是0第二个就是1以此类推...,不标记位置参数默认0->开始
two比one大
实例2:
formatter = "{} {} {} {}"
formatter1 = 1
formatter2 = 2
formatter3 = 3
forma = 4
print(formatter.format(1, 2, 3, 4))
print(formatter.format("one", "two", "three", "four"))
print(formatter.format(True, False, False, True))
print(formatter.format(formatter1,formatter2,formatter3,forma))
print(formatter.format("Try your","Own text here","Maybe a poem","Or a song about fear"))
执行结果:
1 2 3 4
one two three four
True False False True
1 2 3 4
Try your Own text here Maybe a poem Or a song about fear
%d、%s、%f
%d:有符号整数(十进制)
%s :字符串形式
%f:小数
实例:
>>> a = "one"
>>> b = "two"
>>> print("%s比%s大" %(b,a))
two比one大
接收用户输入
格式:变量 = input()
实例1:
print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weigh?", end=' ')
weight = input()
print(f"So, you're {age} old, {height} tall and {weight} heavy")
结果:
How old are you? 18
How tall are you? 180
How much do you weigh? 100
So, you're 18 old, 180 tall and 100 heavy
实例2:
print("请输入你的年龄:",end='')
a = int(input()) #执行到这会等待用户输入
print(f"你的输入的年龄是{a}")
结果:
请输入你的年龄:18 #执行到这会等待用户输入
你的输入的年龄是18
实例3:
age = int(input("How old are you? "))
height = input("How tall are you? ")
weight = input("How much do you weigh? ")
print(f"So, you're {age} old, {height} tall and {weight} heavy")
结果:
How old are you? 18
How tall are you? 180
How much do you weigh? 50
So, you're 18 old, 180 tall and 50 heavy
模块导入
from sys import argv #argv获取当前脚本路径
# read the WYSS section for how to run this
print(argv)
script = argv
print("The script is called:", script)
执行结果:
['D:/xuexi/python练习.py']
The script is called: ['D:/xuexi/python练习.py']
读取文件
格式:open()
实例:
shiyan.txt 的内容是:
小a:我是小a
小b:我是小b
小c:我是小c
def save_file(z,x):
boy = open('D:/a.txt','w')#以写入的方式打开这个文件如不存在会自动添加
girl = open('D:/b.txt','w')
boy.writelines(z)#将z收到的结果写入boy
girl.writelines(x)#将x收到的结果写入girl
boy.close()#写完记得关闭这个文件
girl.close()#写完关闭里面就有了
def set_up(chuanru): #<--入参口
a = open('d:/shiyan.txt')
z = []
x = []
for i in a:
(one,two) = i.split(':',1)# 1代表分割1次
if one == '小a':
z.append(two)#将two的结果添加到z
if one == '小b':
x.append(two)#将two的结果添加到x
save_file(z,x)#在关闭文件前调用传参给sava_file
a.close() #要养成用完关闭的习惯
set_up('d:/shiyan.txt')#调用传参给set_up,括号中可以随便传这里面没用到
##上面这句为调用函数代码,入参口
执行结果:
如果没有 a.txt 和 a.txt 会自动在结果路径中创建
a.txt --> 小a:我是小a
b.txt --> 小b:我是小b
文件打开方式:
模式 可做操作 若文件不存在 是否覆盖
r 只能读 报错 -
r+ 可读可写 报错 是
w 只能写 创建 是
w+ 可读可写 创建 是
a 只能写 创建 否,追加写
a+ 可读可写 创建 否,追加写
函数
格式:
def functionname():
一个简单的函数
def test():
print("This is one function")
a = 1
b = 2
print(a + b)
test() #调用函数
结果:
This is one function
3
可传参的函数
def test(a,b):
print("This is one function")
print(a + b)
test(1,2) #调用函数
结果:
This is one function
3
带默认值的
def test(a,b=2):
print("This is one function")
print(a + b)
test(1) #调用函数
结果:
This is one function
3
设置默认值后也可以传新值:
def test(a,b=2):
print(f"This is one function")
print(a + b)
test(1,3) #调用函数
结果:
This is one function
4
注意: 默认参数只能在非默认参数之后(下面将演示一段错误的代码):
def function(a,b=1,c,d=2): #这样写是错误的,因为非默认参数c不应该出现在b之后,应该在b之前
简单命令,未完结
python简单笔记的更多相关文章
- Python学习笔记2-flask-sqlalchemy 简单笔记
flask-sqlalchemy 简单笔记 字数 阅读 评论 喜欢 flask-sqlalchemy SQLAlchemy已经成为了python世界里面orm的标准,flask是一个轻巧的web框架, ...
- python简介以及简单代码——python学习笔记(一)
学习来源:https://www.liaoxuefeng.com/wiki/1016959663602400 了解python 简单编写并实现python代码 命令行模式和python交互模式 了解p ...
- Web Scraping with Python读书笔记及思考
Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...
- VS2013中Python学习笔记[Django Web的第一个网页]
前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...
- python自学笔记
python自学笔记 python自学笔记 1.输出 2.输入 3.零碎 4.数据结构 4.1 list 类比于java中的数组 4.2 tuple 元祖 5.条件判断和循环 5.1 条件判断 5.2 ...
- [Python爬虫笔记][随意找个博客入门(一)]
[Python爬虫笔记][随意找个博客入门(一)] 标签(空格分隔): Python 爬虫 2016年暑假 来源博客:挣脱不足与蒙昧 1.简单的爬取特定url的html代码 import urllib ...
- OpenCV之Python学习笔记
OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...
- 【Python学习笔记之二】浅谈Python的yield用法
在上篇[Python学习笔记之一]Python关键字及其总结中我提到了yield,本篇文章我将会重点说明yield的用法 在介绍yield前有必要先说明下Python中的迭代器(iterator)和生 ...
- Python学习笔记(十三)
Python学习笔记(十三): 模块 包 if name == main 软件目录结构规范 作业-ATM+购物商城程序 1. 模块 1. 模块导入方法 import 语句 import module1 ...
随机推荐
- Spark集群之yarn提交作业优化案例
Spark集群之yarn提交作业优化案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.启动Hadoop集群 1>.自定义批量管理脚本 [yinzhengjie@s101 ...
- nGrinder TestGroovy.groovy
s /** * */ package com.iteye.lindows.mysql /** * @author Lindows * */ class TestGroovy { static main ...
- Bandicam录制视频
我已经录制了一个视频,关于怎么录制视频的,原画画质的嘻嘻.视频地址 http://www.tudou.com/programs/view/T7xzG1CgsD4 ------------------ ...
- JavaSE学习总结(十六)—— 泛型与泛型应用
一.泛型概要 泛型(Generic)的本质是类型参数化,通俗的说就是用一个占位符来表示类型,这个类型可以是String,Integer等不确定的类型,表明可接受的类型. 泛型是Java中一个非常重要的 ...
- javascript 体验倒计时:距离国庆还有多长时间
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- html5 实时监听输入框值变化的完美方案:oninput & onpropertychange
结合 HTML5 标准事件 oninput 和 IE 专属事件 onpropertychange 事件来监听输入框值变化. H5手机端: <input type="text" ...
- Mybatis中的StatementType
原文:http://luoyu-ds.iteye.com/blog/1517607 要实现动态传入表名.列名,需要做如下修改 添加属性statementType=”STATEMENT” 同时sql里的 ...
- intellj(idea) 编译项目时在warnings 页签框里 报 “xxx包不存在” 或 “找不到符号” 或 “未结束的字符串字面值” 或 “需要)” 或 “需要;”等错误提示
如上图: 环境 是 刚换的系统,重装的Intellj,直接双击老的皇帝项目中的idea的 .iml文件,结果 打开 intellj 后,进行 ctrl +shift +F9 编译时 尽然报 错误提示, ...
- 【51nod】1239 欧拉函数之和 杜教筛
[题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...
- Linux之Ubuntu安装搜狗输入法
1.下载搜狗输入法安装包 搜狗官网:https://pinyin.sogou.com/linux/ 2.更新ubuntu内置的包管理器apt-get的软件源[如果中途安装失败,经常是此原因造成的] s ...