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之前

简单命令,未完结

转载于:https://www.cnblogs.com/weibgg/p/10787078.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值