> 文章列表 > python之路 - 基础1

python之路 - 基础1

python之路 - 基础1

1.安装windows安装双版本Python2,Python3

下载Python2和Python3
https://www.python.org/downloads/

分别安装两个版本

进入Python3的安装目录后,将Python.exe重命名为Python3.exe,删除脚本文件夹下的pip.exe

添加环境变量

Python3安装后已经默认添加到环境变量中,这里只需要添加Python2的环境变量

添加到PATH中

C:\\Python27
C:\\Python27\\Scripts

测试是否成功

在cmd中输入Python显示2,输入Python3显示3,输入pip -V 显示2,输入pip3会报错

这里需要再执行一步

python3 -m pip install -U pip

然后再执行pip3 -V显示3

2.pycharm使用

3.第一个Python程序

print("Hello,world!")

4.变量

 name1 = "tom"name2 = name1print ("name1")print ("name2")name1 = "Jack"print ("name1")print ("name2")

name1=name2时,当name1变量重新赋值的时候,name2不会跟随name1赋值

5.字符编码

中文输入,2中有这个问题,而3中已不存在

 #_*_ coding:utf-8 _*_

6.用户交互

input、raw_input

Python2


name = raw_input("what is your name:")print name

Python3

 name = input("what is your name:")print (name)

7.if else流程判断

number = 27guess_number = int(input("input a number"))if guess_number == number:print ("恭喜,你猜对了")elif guess_number > number:print ("你猜的数字大了")else:print ("你猜的数字小了")

8.while循环

num = 0while num < 3:print ("loop:" % num)num += 1

9.for循环

 for i in range(10):print i

0.循环控制continue、break

continue跳出当次循环,继续下次循环

break跳出整个循环

古诗集