python3基础语法速览——笨方法学python3读书笔记

utf-8编码

# --coding: utf-8--
打印

print("hello world")
print("Hens", 25 + 30 / 6)
print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
print("Is it greater ?", 5 >= -2)
print("It's fleece was white as %s." % 'snow')
print("." * 10)

formatter = "%s %s %s %ss"
print(formatter %(1, 2, 3, 4))
print(formatter %("one", "two", "three", "four"))
print(formatter %(True, False, False, True))
print(formatter %(formatter, formatter, formatter, formatter))
print(formatter %("I had this thing", "that you could", "but it did", "so i said"))

print(
"""
there is something going on here.
with the three double-quotes.
we will be able to type as much as we like
even 4 line if we want, or 5, or 6.
"""
输入

print("how old are you?", end=' '),
age = input()

age = input("How old are you? ")
参数

from sys import argv

script, first, second, third = argv

print("script is: ", script)
print("1st var is: ", first)
print("2nd var is: ", second)
print("3rd var is: ", third)
读写文件

# 读取
from sys import argv

script, filename = argv

txt = open(filename)

print("here is you file %r" % filename)
print(txt.read())
txt.close()

# 读写
from sys import argv

script, filename = argv

print("we are going to erase %r." % filename)
print("if you don't want that, hit ctrl + c.")
print("if you do want that, hit enter")

input("?")

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

print("truncating the file. goodbye!")
target.truncate()

print("now i am going to ask you for three lines.")

line1 = input("1: ")
line2 = input("2: ")
line3 = input("3: ")

print("i am going to write these lines to file.")

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print("finally, we close it")
target.close()

# 文件复制
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))

raw_input = open(from_file)
indata = raw_input.read()

print("the input file is %d bytes long" % len(indata))
print("does the output file exist? %r " % exists(to_file))
print("ready, hit enter to continue, ctrl+c to abort.")
input()

output = open(to_file, 'w')
output.write(indata)

print("ok, all done")

output.close()
raw_input.close()
定义函数

# 简单函数
def print_two(*args):
	arg1, arg2 = args
	print("arg1: %r, arg2: %r" %(arg1, arg2))
	
def print_two_again(arg1, arg2):
	print("arg1: %r, arg2: %r" % (arg1, arg2))
	
def print_one(arg1):
	print("arg1: %r" % arg1)
	
def print_none():
	print("i got nothing")
	
print_two("zed", "shaw")
print_two_again("zed", "shaw")
print_one("first")
print_none()

# 实现算术运算
def add(a, b):
	print("add %d + %d" %(a, b))
	return a + b
	
def sub(a, b):
	print("sub %d - %d" % (a, b))
	return a - b
	
def mul(a, b):
	print("mul %d * %d" % (a, b))
	return a * b
	
def div(a, b):
	print("div %d / %d" %(a, b))
	return a / b
	
print("let's do some math with just functions")

age = add(30, 5)
height = sub(78, 4)
weight = mul(30, 2)
iq = div(100, 2)

print("values: ", age, height, weight, iq)

print("here is a puzzle")
what = add(age, sub(height, mul(weight, div(iq, 2))))

print("values: ", what, " you right?")
分支

people = 20
cats = 30
dogs = 15

if people < cats:
	print("too many cats")
	
if people > cats:
	print("not too many cats")
	
if people < dogs:
	print("too many dogs")
	
if people > dogs:
	print("ok")
	
dogs += 5

if people >= dogs:
	print("people are greater than equal to dogs")
	
if people <= dogs:
	print("people are less than or equal to dogs")
	
if people == dogs:
	print("people are dogs")
循环

the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

for number in the_count:
	print("this is count %d" % number)
	
for fruit in fruits:
	print("a fruit of type: %s" % fruit)
	
for i in change:
	print("i got %r" % i)
	
elements = []

for i in range(0, 6):
	print("adding %d to the list." % i)
	elements.append(i)
	
for i in elements:
	print("element was: %d" %i)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值