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简单笔记的更多相关文章

  1. Python学习笔记2-flask-sqlalchemy 简单笔记

    flask-sqlalchemy 简单笔记 字数 阅读 评论 喜欢 flask-sqlalchemy SQLAlchemy已经成为了python世界里面orm的标准,flask是一个轻巧的web框架, ...

  2. python简介以及简单代码——python学习笔记(一)

    学习来源:https://www.liaoxuefeng.com/wiki/1016959663602400 了解python 简单编写并实现python代码 命令行模式和python交互模式 了解p ...

  3. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  4. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  5. python自学笔记

    python自学笔记 python自学笔记 1.输出 2.输入 3.零碎 4.数据结构 4.1 list 类比于java中的数组 4.2 tuple 元祖 5.条件判断和循环 5.1 条件判断 5.2 ...

  6. [Python爬虫笔记][随意找个博客入门(一)]

    [Python爬虫笔记][随意找个博客入门(一)] 标签(空格分隔): Python 爬虫 2016年暑假 来源博客:挣脱不足与蒙昧 1.简单的爬取特定url的html代码 import urllib ...

  7. OpenCV之Python学习笔记

    OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...

  8. 【Python学习笔记之二】浅谈Python的yield用法

    在上篇[Python学习笔记之一]Python关键字及其总结中我提到了yield,本篇文章我将会重点说明yield的用法 在介绍yield前有必要先说明下Python中的迭代器(iterator)和生 ...

  9. Python学习笔记(十三)

    Python学习笔记(十三): 模块 包 if name == main 软件目录结构规范 作业-ATM+购物商城程序 1. 模块 1. 模块导入方法 import 语句 import module1 ...

随机推荐

  1. CentOS 6.9/7通过yum安装指定版本的MySQL

    一.安装CENTOS 6 # wget http://repo.mysql.com/mysql57-community-release-el6.rpm && rpm -ivh mysq ...

  2. LVS+keepalived 的DR模式的两种做法

    LVS DR模式搭建 准备工作 三台机器: dr:192.168.13.15 rs1:192.168.13.16 rs2: 192.168.13.17 vip:192.168.13.100 修改DR上 ...

  3. Networx蓝屏问题

    本人系统win7专业版64位. 从5月底开始就时不时有蓝屏发生,而且可以说是没有任何征兆就"啪"的一下蓝了... 有时候是隔个四五天蓝屏一次,有时候一天都能蓝好几次,实在是让人恼火 ...

  4. Python中变量的属性以及判断方法

    1.变量的属性 在Python中,创建一个变量会给这个变量分配三种属性: id ,代表该变量在内存中的地址: type,代表该变量的类型: value,该变量的值: x = 10 print(id(x ...

  5. 选择排序算法的JAVA实现

    1,采用选择排序对元素进行排列时,元素之间需要进行比较,因此需要实现Comparable<T>接口.即,<T extends Comparable<T>>. 更进一 ...

  6. 20155332 2016-2017-2 《Java程序设计》第9周学习总结

    20155332 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 了解JDBC架构 掌握JDBC架构 掌握反射与ClassLoader 了解自定义泛型和自定义 ...

  7. JavaScript之form表单的序列化和json化[form.js]

    一.应用场景 form提交时,使用ajax提交. 二.效果 通过本工具,实现表单所有form的快速序列化和json化,使前端人员在ajax提交form表单的时,脱离重复性的,大劳动量的手动抽取form ...

  8. Java ArrayList类

    ArrayList对象可以用于存储一个对象列表 例子: ArrayList<String> list = new ArrayList<String>() 例子: public ...

  9. Spring+Struts+Mybatis+Shiro整合配置

    Jar包

  10. Window和document的区别

    1.window 窗口对象.就是可视化区域的大小,不包含滚动条内东东. 2.document 对象,包含滚动条以外的区域