笨办法学Python 笔记1(ex1~ex17)

一 Windows中使用powershell使用Python

Python2和3在powershell的切换方法
我的电脑默认打开的是Python3
在这里插入图片描述可以输入py-2 进入Python2
在这里插入图片描述
在powershell打开Python文件 指令为py filename 需要用python2是 将py更换为py -2即可

学习基础语法的时候 我使用的编辑器为notepad++
二 从print开始

print"i'd much rather you 'not'."
print'i"said"do not touch this .'

print没什么说的 对我来说最直观的就是比Python3少了括号

注释还是用# 多行注释就在每一行前面加#

顺便提一句 注释里有汉语在代码开头加上`#--coding:utf-8 --

这里面的#不是注释`

三 数字和数字计算

print"I will now count my chickens:"

print"Hens", 25+30/6
print"Roosters", 100-25*3%4

print"Now i will count that eggs:"

print 3 + 2 + 1 - 5 + 4%2-1.0/4+6

print"Is it true that 3 + 2 <5 - 7?"

print 3+2<5-7

print"what is 3+2?",3+2
print"what is 5-7",5-7

print"oh.,that's why it's false."

print"how about some more."

print"is it greater?",5>-2
print"is it greater or equal?",5>=-2
print"is it less or equal?",5<=-2



运算符号
在这里插入图片描述
运算优先级
PEMDAS(括号 指数 乘除 加减)

整形(int)的除法结果自动取整

四 变量和命名

cars = 100
space_in_a_car = 4.0 
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven


print"There are",cars,"cars available."
print"There are only",drivers,"drivers available."
print"There will be ",cars_not_driven,"empty cars today."
print"We can transport",carpool_capacity,"people today."
print"We have",passengers,"to carpool today."
print"We need",average_passengers_per_car,"in each car."






TIPS:
_是变量中假想的空格

=用来赋值 ==用于检查两边是否相等

变量名要以字母开头

五 字符串和文本

print"."*10
a1 = "c"
a2 = "h"
a3 = "e"
a4 = "e"
a5 = "s"
a6 = 'e'
a7 = 'B'
a8 = 'u'
a9 = 'r'
a10 = 'g'
a11 = 'e'
a12 = 'r'

print a1+a2+a3+a4+a5+a6,
print a7+a8+a9+a10+a11+a12
#-*-coding:utf-8 -*-
formatter = "%r %r %r %r"

print formatter % (1,2,3,4)
print formatter % ("one","two","three","four")
print formatter % (formatter,formatter,formatter,formatter)
print formatter % ("I had this thing","That you could type up right","But it didn't sing","So I said goodnight")
#print后面的要么是已经赋值的变量要么是数字
tabby_cat = "\t I'm tabbed in."
persian_cat = "I'm split\non a line"
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat Food
\t* Finishes
\t* Catnip\n\t* Grass
"""

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
while True:
	for i in ["/","-","|","\\","|"]:
		print"%s\r" %i,

想打印一个字符串变量用%s 在想获取某些调试信息(原始表示)的时候才用%r

(1)字符串格式代码
在这里插入图片描述(2)转义序列
在这里插入图片描述

六 输入

# -*- coding:utf-8-*-
age = raw_input("age")
height = raw_input("height")
weight = raw_input("weight")

print "%r\n %r\n %r\n"%(age,height,weight)


#rawinput处理的是输入的内容 input处理的是输入的代码 可能会产生注入漏洞



七 参数、解包和变量

# -*- coding:utf-8-*-
from sys import argv

script,first,second,third = argv

print"The script is called :",script
print"Your first variable is:",first
print"Your second variable is:",second
print"Your third variable is:",third
#给参数赋值在命令行中进行

像这样
在这里插入图片描述巩固练习(提示和传递)

# -*- coding:utf-8-*-
from sys import argv

script,user_name = argv
prompt = ">"

print"Hi %s ,I'm the %s script ."%(user_name,script)
print"I'd like to ask you a few questions."
print"Do you like me %s?"%user_name
likes = raw_input(prompt)

print "Where do you live %s?"%user_name
lives = raw_input(prompt)

print"What kind of computer do you have?"
computer = raw_input(prompt)

print"""
Alright,so you said %r about liking me.
You live in %s. Not sure where that is.
And you have a %s computer. Nice.
"""%(likes,lives,computer)
#注意回显 %r返回的是变量原始数据(raw data) %s是用来向用户输出的

效果如图

(1)读取文件

from sys import argv

script,filename = argv

txt = open(filename)

print"Here's your file %r:" %filename
print txt.read()

#print"Type the filename again:"
#file_again = raw_input(">")

#txt_again = open(file_again)

#print txt_again.read()

我创建了一个名为ex15.txt的文件 效果如图

在这里插入图片描述
(2)读写文件

close 关闭文件 (跟文件编辑时的文件->保存一个意思)
read 读取文件内容(可以将结果赋值给一个变量)
readline 读取文本文件中的一行
truncate 清空文件
write(stuff) 将stuff写入文件

# -- coding: utf-8 --
from sys import argv

script,filename = argv

print"We 're going to erase %r."%filename
print"If you don't want that ,hit CTRL-C(^C)."
print"If you do want that,hit RETURN."

raw_input("?")

print"Open the file......"
target = open(filename,'w')

print"Truncating the file.Goodbye!"
target.truncate()
#清空文件
print"Now I'm going to ask you for three lines."

line1 = raw_input("line1:")
line2 = raw_input("line2:")
line3 = raw_input("line3:")

print"I'm going to write these to the file."

target.write("%s\n%s\n%s\n"%(line1,line2,line3))


target = open(filename)#不要忘了重新open一下 而且变量貌似只能用target 别的都不可以。
print(target.read())
target.close()


print"And finally ,we close it."
target.close()#open(路径/文件名,mode)
#mode:#读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式

注意:
read之前先用open打开

open 对于文件的写入操作态度是安全第一,所以你只有特别指定以后,它才会进行写入操作

因为open()默认是’r’,即读取文本模式,不能进行写入; 所以需要指定写入模式, 用到参数w。

我创建了名为ex16.txt的文件 效果如图
在这里插入图片描述(3)文件内容转移

# -- coding: utf-8 --
from sys import argv
from os.path import exists

script,from_file,to_file = argv

print"Copying from %s to %s"% (from_file,to_file)

in_file = open(from_file),
indata = in_file.read()

print"The input file is %d bytes long" % len(indata)

print"Does the output file exist? %r" %exists(to_file)
print"Ready , hit RETURN to continue ,CTRL-C to abort."
raw_input()

out_file = open(to_file,'w')
out_file.write(in_data)

print"Alright,all done."

out_file.close()
in_file.close()

在这里插入图片描述out_file = open(to_file, ‘w’) 执行时会创建 to_file 文件,但是没内容

out_file.write(indata) 执行时,内容会写入到 to_file 的内存数据中,但仍未写入硬盘。

只有在执行 close 时 python 才指定文本的各种操作已经结束了,不会再有任何变化,这个时候在写入硬盘可以尽可能地减少硬盘读写操作,提高效率(特别在特大文件的时候)

To be cotinued ```````

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值