python 笔记 基础知识回顾 《笨办法学Python》习题22 ——12.29

这篇博客回顾了作者在学习《笨办法学Python》时,通过习题22所掌握的基础知识,包括Python的重要概念和技能。
摘要由CSDN通过智能技术生成

习题 22: 到现在你学到了哪些东西?

知识回顾

1.print""   #打印
2.#运算符号:
  • + plus 加号
  • - minus 减号
  • / slash 斜杠
  • * asterisk 星号
  • % percent 百分号
  • < less-than 小于号
  • > greater-than 大于号
  • <= less-than-equal 小于等于号
  • >= greater-than-equal 大于等于号

3.#-*- coding: utf-8 -*-   #声明编码,具体用法为   coding:[编码]或者coding=[编码]

4.cars = 10
  10cars = 10 * cars 
#变量(variable) ,由数字字母和字符组成,开头须以英文开头

5.#字符串格式化输出,#目前常用的有 
%s 字符串 
%d 有符号整数 十进制
f 浮点数字(用小数点符号
%r 字符串 (采用repr()的显示

例:
tt = 13.555
print "you have %6.2f apples" %tt

5.#转义序列,即使转义成某些符号
#目前常用的有
\\  Backslash()反斜杠
\'  Single quote (‘)  单引号
\"  Double quote(”)  双引号
\n  ASCII Linefeed (LF)  换行符
\r  ASCII Carriage Return (CR) 回车符
\t  ASCII Horizontal Tab (TAB)  水平制表符

6.#无限循环
while True:
	for i in ["/","-","|","\\","|"]:
		print "%s \r" % i,

7.raw_input()
#读取控制台输出
例:
print "what do you like?",
this = raw_input()
print"Oh,yes. I think %r is very good!"%this

#还可以于括号中输入字符用于提示
例:
age = raw_input("How old are you? ")	

8.argv 参数变量 (argument variable)
#若要使用它,首先从sys声明
from sys import argv

script, user_name = argv 
#解包,script是脚本(xxx.py)文件名
#first是参数,由用户输入,其可以是字符,也可以是文件

print "Do you like me %s?" % user_name

9.#对文件进行操作
 • close –  关闭文件。跟你编辑器的 文件 -> 保存 ..  一个意思。
 • read –  读取文件内容。你可以把结果赋给一个变量。
 • readline –  读取文本文件中的一行。
 • truncate –  清空文件,请小心使用该命令。
 • write(stuff) –  将 stuff 写入文件
 • seek
 
 例:
from sys import argv
script, from_file,to_file = argv

in_file = open(from_file)
read_file = in_file.read()
out_file = open(to_file,'w')
out_file.write(read_file)
out_file.close()
in_file.close()

#如果只需写入2行,并在第三个自定义写入:
from sys import argv
script, from_file,to_file = argv

in_file = open(from_file)
readline_file = in_file.readline()
readline_file2 = in_file.readline()
out_file = open(to_file,'w')
out_file.write(readline_file)
out_file.write(readline_file2)
out_file.write("shou dong shu ru!")

out_file.close()
in_file.close()

10.#定义函数 def,可以使用字符串或者变量
#字符串
def print_two_again(arg1, arg2):
	print "arg1: %r, arg2: %r" % (arg1, arg2)
#变量
def cheese_and_crackers(cheese_count, boxes_of_crackers):
	print "You have %d cheeses!" % cheese_count
	print "You have %d boxes of crackers!" % boxes_of_crackers

	
11.#seek和readline
#内建函数seek(offset,whence=0)
#Python文件的seek()方法将文件的当前位置设置为偏移量(offset)。
#offset:开始的偏移量,也就是代表需要移动偏移的字节数
#whence:给offset参数一个定义,表示要从哪个位置开始偏移;
#默认为0,0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。

def rewind(f):
	f.seek(0)
	
12.#函数中 return的使用, return理解为结果是,返回但是divide是函数,并不直接返回结果,可以理解为定义的运算方式
例:返回a/b的结果
def divide(a, b):
	print "DIVIDING %d / %d" % (a, b)
	return a / b
iq = divide(100, 2)
print " IQ: %d" % iq


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值