Python安装与初识
Python初识
python的优势:开发效率高
劣势:执行效率低
python的种类:JPython ,CPython ,pypy
python之禅:(自行谷歌翻译)
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
python安装
Windows安装:
2.在本地安装
3.设置环境变量
右键-> 我的电脑 -> 属性 -> 高级系统设置 -> 环境变量 -> 系统变量 -> Path -> 新建 -> python软件下bin文件夹
4.cmd测试
python编译python文件
#a.py为python代码文件 python a.py
Linux安装:
yum 安装 yum install python
apt安装 apt-get install python
......
python基础
变量
//一:字符串 //四种形式字符串 //1 name='Hello World!' //2 name="Hello World!" //3 name="""Hello World!""" //4 name='''Hello World!''' //二:数字 num=123
输出
//控制台输出 print("Hello World!") //控制台变量输出 name="Hello World!" print(name)
输入
//控制台输入 num=input("请输入你的年龄?") //输出年龄 print("我的年龄是%d"%(int(num)));
逻辑判断
if
//if作用是判断 age=input("请输入你的年龄?") if age < 12 : print("儿童") elif age < 18 : print("少年") else : print("青年")
循环
while
//while循环 #coding=gbk num=1 while num < 10: print("循环第%d次!"%(num)) num = num + 1
for
#coding=gbk //for range()循环数字 for i in range(10): print(i) //for循环字符串 for i in "Hello World!": print(i)
Test
从1循环到100
#coding=gbk '''从1循环到10''' for i in range(100): print(i)
从1加到100
#coding=gbk ''' 1+2+3+....+100 ''' num=0 for i in range(100): num+=i print(num)
1-2+3-4+5-6+....-100
#coding=gbk ''' 1-2+3-4+...-100 ''' num = 0 for i in range(100): if i % 2 == 0 : num=num-i else: num=num+i print(num)
用户登录,三次机会重试
#coding=gbk '''用户登录,三次机会重试 ''' num = 0 while num < 3: user_name = input("请输入用户名:") if user_name == "tom" : print("登陆成功!") num=3 else : print("登陆失败!") num = num + 1 print("还有%d次机会!"%(3-num))