python之路 - 基础1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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程序

技术分享
1 print("Hello,world!")
View Code

 

4.变量

技术分享
1 name1 = "tom"
2 name2 = name1
3 print ("name1")
4 print ("name2")
5 
6 name1 = "Jack"
7 print ("name1")
8 print ("name2")
View Code

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

 

5.字符编码

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

技术分享
1 #_*_ coding:utf-8 _*_
View Code

 

6.用户交互

input、raw_input

Python2

技术分享
1 name = raw_input("what is your name:")
2 print name
View Code

Python3

技术分享
1 name = input("what is your name:")
2 print (name)
View Code

 

7.if else流程判断

技术分享
 1 number = 27
 2 
 3 guess_number = int(input("input a number"))
 4 
 5 if guess_number == number:
 6     print ("恭喜,你猜对了")
 7 elif guess_number > number:
 8     print ("你猜的数字大了")
 9 else:
10     print ("你猜的数字小了")
View Code

 

8.while循环

技术分享
1 num = 0
2 
3 while num < 3:
4     print ("loop:" % num)
5     num += 1
View Code

 

9.for循环

技术分享
1 for i in range(10):
2     print i
View Code

 

10.循环控制continue、break

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

break跳出整个循环



以上是关于python之路 - 基础1的主要内容,如果未能解决你的问题,请参考以下文章

Python高手之路python基础之模块

python基础成长之路四-基础数据类型方法

python之路之前没搞明白4面向对象(封装)

Python之路,第一篇:Python入门与基础

基础篇D1 -- Python学习之路

Python之路,Day4 - Python基础4