1.python介绍
2.安装
3.第一个python程序
4.变量
5.用户输入
1.python介绍
根据 IEEE Spectrum 发布的研究报告显示,在 2016 年排名第三的 Python 在今年已经成为世界上最受欢迎的语言,C 和 Java 分别位居第二和第三位。由此看出python将是未来语言
2.安装
首先我们进入https://www.python.org 中下载与你系统对应版本的python2.7或者python3.5
3.第一个python程序
运行python,建立一个Hello,word程序。
4.变量\\字符编码
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
声明变量
1
2
3
|
#_*_coding:utf-8_*_ name = "Alex Li" |
上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li"
变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
[‘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‘]
变量的赋值
1
2
3
4
5
6
7
8
|
name = "Alex Li" name2 = name print (name,name2) name = "Jack" print ( "What is the value of name2 now?" ) |
注释
当行注视:# 被注释内容
多行注释:""" 被注释内容 """
5.用户输入
1
2
3
4
5
6
7
|
#!/usr/bin/env python #_*_coding:utf-8_*_ #name = raw_input("What is your name?") #only on python 2.x name = input ( "What is your name?" ) print ( "Hello " + name ) |
输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/env python # -*- coding: utf-8 -*- import getpass # 将用户输入的内容赋值给 name 变量 pwd = getpass.getpass( "请输入密码:" ) # 打印输入的内容 print (pwd) |