http://www.cnblogs.com/alex3714/articles/5465198.html

四、Python安装

windows

1
2
3
4
5
6
7
1、下载安装包
    https://www.python.org/downloads/
2、安装
    默认安装路径:C:\python27
3、配置环境变量
    【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
    如:原来的值;C:\python27,切记前面有分号

linux、Mac

1
2
3
无需安装,原装Python环境
  
ps:如果自带2.6,请更新至2.7

  

五、Hello World程序

在linux 下创建一个文件叫hello.py,并输入

1
print("Hello World!")

然后执行命令:python hello.py ,输出

1
2
3
localhost:~ jieli$ vim hello.py
localhost:~ jieli$ python hello.py
Hello World!

指定解释器

上一步中执行 python hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行。

如果想要类似于执行shell脚本一样执行python脚本,例: ./hello.py ,那么就需要在 hello.py 文件的头部指定解释器,如下:

1
2
3
#!/usr/bin/env python
  
print "hello,world"

如此一来,执行: ./hello.py 即可。

ps:执行前需给予 hello.py 执行权限,chmod 755 hello.py

在交互器中执行 

除了把程序写在文件里,还可以直接调用python自带的交互器运行代码, 

1
2
3
4
5
6
localhost:~ jieli$ python
Python 2.7.10 (default, Oct 23 201518:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help""copyright""credits" or "license" for more information.
>>> print("Hello World!")
Hello World!

对比下其它语言的hello world

六、变量\字符编码  

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?")

 

七、字符编码

python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)

ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,所以,ASCII码最多只能表示 255 个符号。

小结:

  ASCII 255 1bytes

    --> 1980 gb2312  7xxx

      --> 1995 gbk1.0  2w+

        --> 2000  gb18030  27xxx

      --> unicode 2bytes

        --> utf-8  en:1byte, zh:3bytes

注释

  当行注视:# 被注释内容

  多行注释:""" 被注释内容 """

八、用户输入 

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)

 

多行格式化:

  1. # Author: sonny# -*- coding:utf-8 -*-print("hello world!");
  2.  
  3. #注: python2.x raw_input python3.x inputname = input("name:");age = int(input("age:"));job = input("job:");info = ''' ---- info of %s ---- name = %s age = %d job = %s''' % (name, name, age, job);
  4.  
  5. info2 = ''' ---- info of {_name} ---- name = {_name} age = {_age} job = {_job}'''.format(_name=name, _age=age, _job=job);
  6.  
  7. info3 = ''' ---- info of {0} ---- name = {0} age = {1} job = {2}'''.format(name,age,job);print(info);print(info2);print(info3);
  1. ifwhilefor实例:
  1. # Author: sonny# -*- coding:utf-8 -*-
  2.  
  3. age = 28;
  4.  
  5. for count in range(3): _age = int(input("guess age:")); if _age == age: print("you got it!"); break; elif _age > age: print("guess smaller"); else: print("guess bigger!") count += 1; if count == 3: key = input("are you want contuine?"); if key != 'n': count = 0;'''count = 0;while count<3: _age = int(input("guess age:")); if _age == age: print("you got it!"); break; elif _age > age: print("guess smaller"); else: print("guess bigger!") count += 1; if count ==3: key = input("are you want contuine?"); if key != 'n': count =0;#else:# print("you are wrong,fuck off!");'''
  1.  

2017day1的更多相关文章

  1. 集训Day1

    雅礼集训2017Day1的题 感觉上不可做实际上还挺简单的吧 T1 区间加 区间除法向下取整 查询区间和 区间最小值 大力上线段树,把除法标记推到底,加法标记就是按照线段树的来 先拿30 然后60的数 ...

随机推荐

  1. 百度 Echarts 地图表 js 引用路径

    使用地图表格,除了需echarts,还需zrender,自行下载JS文件:   目标,做成这样的效果:http://echarts.baidu.com/doc/example/map3.html   ...

  2. Delphi线程类 DIY(把类指针作为参数传进去,就可以执行类里面的方法啦)

    Delphi 封装了一个很强大的线程类 TThread, 我们也自己动手制作一个简单的线程类 首先Type一个类 type TwwThread = class constructor Create;  ...

  3. 测试 Components 与 Controls 的区别(嵌套在Panel上的Edit,依然是Form1的Component)

    本例效果图: 代码文件: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  4. 使用 Gitlab CI/CD 实现自动化发布站点到 IIS

    说明 这里先介绍下两个东西 CI/CD.GitLab Runner,当然在此之前你需要对 git 有所了解,关于 git 这里不做说明,可以自行百度. 首先介绍 CI/CD :随着我们开发方式的转变, ...

  5. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  6. Spark学习之路(十四)—— Spark Streaming 基本操作

    一.案例引入 这里先引入一个基本的案例来演示流的创建:获取指定端口上的数据并进行词频统计.项目依赖和代码实现如下: <dependency> <groupId>org.apac ...

  7. spring cloud 系列第2篇 —— eureka 高可用注册中心的搭建 (F版本)

    源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.项目结构 eureka-server为服务注册中心,负责服务的管理: eur ...

  8. Mac下安装redis5.0 与命令

    参考链接:https://blog.csdn.net/zyp1376308302/article/details/84257606 参开链接2:https://www.cnblogs.com/guan ...

  9. Appcan 自定义数字加减控件

    DIV部分: *这里的三个ID:as_sub_3.as_now_3.as_add_3里面的“3”可以自定义,这个对于生成任意个数的列表形式很有帮助 *cb 为执行成功后可进行回调 <div cl ...

  10. GO代码生成代码小思小试

    推进需求 GO 项目,可整体生成一个运行文件到处跑,是极爽之事.但如果有资源文件要得带着跑,则破坏了这种体验. 例如下边这个项目结构,resource 目录下为资源文件,main.go 中会通过路径引 ...