Python之路-python环境安装和简单的语法使用

一、环境安装

Win10(安装python3.x):

1、下载安装包

    https://www.python.org/downloads/
 
2、安装完成后设置环境变量。 
    桌面-“计算机”-右键“属性”-“高级系统设置”-“高级”-“环境变量”-“系统变量”。找到Path,在后面加上python3.x安装路径下(记得用分号隔开)例                  如;C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32

 

Centos 6.5安装:

  yum install zlib-devel bzip2-devel openssl-devel ncurese-devel gcczlib

  下载地址:https://www.python.org/downloads/release/python-363/

  解压:tar zxvf Python-3.6.3.tgz

  #./configure

  #make

  #make install

 

Centos 7.2.1511:

1、下载tar.gz安装包。

2、安装python3.x(因为centos7自带python版本是2.7.5,所以这里做的只是安装python3.x,而不删除原来版本)

首先需要yum安装gcc、make

#./configure --prefix=/usr/local/python3

#make && make install

#ln -s /usr/local/python3/bin/python3 /usr/bin/python3(做一个软件链接)

结果:

[root@localhost /]# python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[root@localhost /]# python3
Python 3.4.3 (default, Jul 22 2016, 14:29:49)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

二、Hello World程序

1、Linux下创建一个helloword.py的文件,并执行chmod 755 helloword.py(给一个可以执行的权限)

      #!/usr/bin/python#(linux下执行的话,如果不指出解释器的话需要python3  helloword.py,这里我们指出了解释器,所以可直接./helloword.py)

      print("Hello Word")

  [root@localhost test]# ./hellword.py
  Hello Word

2、Windows下,这里不再介绍Pycharm安装。

  print("Hello Word")

三、变量\变量赋值(这里用Pycharm运行)

声明变量:

#coding=utf-8

name = “Hello China”

上述中name是变量名,“Hello China”是给变量name赋的值。

这里强调一下,变量名的命名规则,只能是字母(大小写)、数字、下划线的任意组合(变量名第一个字符不能是数字)。

注意(有些关键字不能是变量名):

如:'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'。

四、字符编码

如何在python中使用中文呢,回顾字符编码,从ASCII到GB2302到GBK1.0到GB18030到Unicode。(UTF-8是对Unicode编码的一种优化和压缩),简单的说如果需要Python中显示中文,需要加上#coding=utf-8,python3中默认使用utf-8编码,所以不用加。

[root@localhost test]# python3 hellword.py
你好 中国

 

七、用户输入

#!/usr/bin/python
#coding:utf8
name = input("what your name?")
print("Hello",name)

[root@localhost test]# python3 hellword.py

what your name?Mr.Lei
Hello Mr.Lei

如果用户在输入密码的时候想不显示,可以使用python的getpass模块

#!/usr/bin/python
#coding:utf8

import getpass
name = input("用户名")
pwd = getpass.getpass("登陆密码")
print(name,pwd)

[root@localhost test]# python3 hellword.py
用户名Mr.lei
登陆密码
Mr.lei 123

 

八、流程控制

1、if 表达式

#!/usr/bin/env python
#coding:utf8

_name = "zhangsan"
_pass = "123456"

name = input("用户名")
pwd = input("登陆密码")
if name == _name and pwd == _pass:#其中and表示前后两个条件都满足的话执行下面语句,if语句最后要以冒号结束,(==是等于的意思)
print("Welcome",name)

2、for循环

简单的for循环

#!/usr/bin/env python

#coding=utf-8
for i in (0,1,2,3,4):#从上到下依次打印0 1 2 3 4
print(i)

可以再精简一下,如下

 

#!/usr/bin/env python
#coding=utf-8
for i in range (0,5):#如果想打印到100的话,就将5改成100
print(i)

 

如果想打印0到100的偶数的话如下:

 

#!/usr/bin/env python
#coding=utf-8
for i in range (0,100,2):#其中2是步长,意思是间隔两个数打印一下。
print(i)

 

3、while循环#!/usr/bin/env python#coding=utf-8

conut = 0
while True:
print(0)
conut += 1
if conut == 10:
break

4、猜分数游戏:#!/usr/bin/env python
#coding=utf-8
math = 86
conut = 0
while conut < 3:

guess_input = int(input("Please enter a score:"))
if guess_input == math:
print("Right")
break
elif guess_input > math:
print("Bigger")
else:
print("smaller")
conut += 1
if conut == 3:
_input = input("Whether to continue?n=Sign out,Other=Continue")
if _input != 'n':
conut = 0
else:
print("fuck out")

break:跳出本次循环
pass:通过
continue:跳出本次循环,进行下一轮循环
exit():结束整个循环

  
posted @ 2016-07-25 11:49  Charles.L  阅读(1215)  评论(0编辑  收藏  举报