2017day1
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:\python273、配置环境变量 【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为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.pylocalhost:~ jieli$ python hello.pyHello 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$ pythonPython 2.7.10 (default, Oct 23 2015, 18:05:06)[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwinType "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 = nameprint(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.xname = 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) |
多行格式化:
# Author: sonny# -*- coding:utf-8 -*-print("hello world!");
#注: 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);
info2 = ''' ---- info of {_name} ---- name = {_name} age = {_age} job = {_job}'''.format(_name=name, _age=age, _job=job);
info3 = ''' ---- info of {0} ---- name = {0} age = {1} job = {2}'''.format(name,age,job);print(info);print(info2);print(info3);
if、while、for实例:
# Author: sonny# -*- coding:utf-8 -*-
age = 28;
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!");'''
2017day1的更多相关文章
- 集训Day1
雅礼集训2017Day1的题 感觉上不可做实际上还挺简单的吧 T1 区间加 区间除法向下取整 查询区间和 区间最小值 大力上线段树,把除法标记推到底,加法标记就是按照线段树的来 先拿30 然后60的数 ...
随机推荐
- 【转】简单的jQuery插件开发方法
在实际开发工作中,总会碰到像滚动,分页,日历等展示效果的业务需求,对于接触过jQuery以及熟悉jQuery使用的人来说,首先想到的肯定是寻找现有的jQuery插件来满足相应的展示需求.目前页面中常用 ...
- 深入浅出RPC——浅出篇(转载)
本文转载自这里是原文 近几年的项目中,服务化和微服务化渐渐成为中大型分布式系统架构的主流方式,而 RPC 在其中扮演着关键的作用. 在平时的日常开发中我们都在隐式或显式的使用 RPC,一些刚入行的程序 ...
- logstash performance testing
最近一直在和peformance team的同事做logstash 5.6.2的测试,主要测试两个方面:一方面测试log数据是否能全部被logstash获取与发出去,一方面测试logstash自身的c ...
- LeetCode : Given a string, find the length of the longest serial substring without repeating characters.
Given a string, find the length of the longest serial substring without repeating characters. Exampl ...
- Hadoop集群(第4期)VSFTP安装配置
1.VSFTP简介 VSFTP是一个基于GPL发布的类Unix系统上使用的FTP服务器软件,它的全称是Very Secure FTP 从此名称可以看出来,编制者的初衷是代码的安全. 安全性是编写VSF ...
- django自带的cache
cache语法 from django.core.cache import cache #存入内存 cache.set("aaa",123) #从内存中获取 cache.get(& ...
- python中的基本数据类型之 int bool str
一.基本数据类型 1. int ==> 整数.主要用来进行数学运算. 2.str ==> 字符串.可以保存少量的数据,并进行相应的操作. 3.bool => 布尔值.判断 ...
- SYN4102型 GPS同步时钟
SYN4102型 GPS同步时钟 产品概述 SYN4102型GPS同步时钟是由西安同步电子科技有限公司精心设计.自行研发生产的一款高精度锁相时钟频率源,接收GPS信号,使恒温晶振输出频率同步于GPS ...
- express 中间件的理解
nodejs(这指express) 中间件 铺垫: 一个请求发送到服务器,要经历一个生命周期,服务端要: 监听请求-解析请求-响应请求,服务器在处理这一过程的时候,有时候就很复杂了,将这些复杂的业务拆 ...
- RequestMappingHandlerAdapter和RequestParam原理分析
我们要使用定义了RequestMapping方法或者类是,需要先准备好所需要的参数.如何准备参数,我们应该考虑些上面问题. 都有哪些参数需要绑定? 除了方法确定的参数,还有两个方法的参数需要绑定,那就 ...