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. VirtualBox设置共享文件夹

    前提是已经正确安装增强工具,在安装增强工具时,没有faile的,全部done 1.添加共享文件夹(已经在lmg下创建过目录 /mnt/bdshare ) sudo mount -t vboxsf Ba ...

  2. CodeForces - 348D Turtles(LGV)

    https://vjudge.net/problem/CodeForces-348D 题意 给一个m*n有障碍的图,求从左上角到右下角两条不相交路径的方案数. 分析 用LGV算法.从(1,1)-(n, ...

  3. Spring RedisTemplate操作-注解缓存操作(11)

    @Service @CacheConfig(cacheNames="user") public class RedisAn { public Map<String, User ...

  4. 字符串数字转换成对应的Double数值

    一,介绍 前面实现了字符串转换成整形数值.参考这里: 它不支持小数,不支持符号(正.负号) 现在实现一个更复杂一点字符串转换成数值的程序. 它支持“浮点字符串”转换成对应的浮点数值,如: " ...

  5. js 碰撞 + 重力 运动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Nginx proxy开启cache缓存

    proxy_temp_path /tmp/proxy_temp_dir; // 设置缓存位置 proxy_cache_path /tmp/proxy_cache_dir levels = : keys ...

  7. string字符串js操作

    String 对象方法 方法 描述 anchor() 创建 HTML 锚. big() 用大号字体显示字符串. blink() 显示闪动字符串. bold() 使用粗体显示字符串. charAt() ...

  8. $_SERVER 当前信息

    连接:https://www.cnblogs.com/mafeng/p/5868117.html $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['R ...

  9. luogu P3707 [SDOI2017]相关分析

    传送门 对于题目要求的东西,考虑拆开懒得拆了 ,可以发现有\(\sum x\sum y\sum x^2\sum xy\)四个变量影响最终结果,考虑维护这些值 下面记\(l,r\)为区间两端点 首先是区 ...

  10. mysql 案例 ~ pt-io工具的使用

    一 简介:如何使用pt-iopfile调查io具体信息二 目的:利用pt-iopfile分析mysql内部IO操作密集的文件,用以发现问题三 使用: pt-iopfile -p mysql_pid   ...