一 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
#给参数赋值在命令行中进行
像这样(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()
To be cotinued ```````