码迷,mamicode.com
首页 > 编程语言 > 详细

Day1 初识Python

时间:2016-06-19 16:52:46      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

(1)变量与赋值

name = "wanghuafeng"
age = 29
print(name, age)

a和b交换值

a = 3
b = 5
tmp = a
a = b
b = tmp
print(a, b)

(2)字符编码

ASCII码表(2**8=128个字符)

技术分享技术分享

unicode:统一码、万国码

每一个字符最少用2个字节(16位)来存储,即2**16=65536

utf-8:是针对unicode的可变长度元编码。

它可以使用1~4个字节表示一个符号,根据不同的符号而变化字节长度。

(3)Pycharm修改文件模板

依次打开:File—>Settings—>Editor—>File and Code Templates—>Python Scripts

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: WangHuafeng

(4)多行打印

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: wanghuafeng
name = input("input your name:")
age = int(input("input your age:"))
job = input("input your job:")
#print("name is:", name + "\nage is:", age + "\njob is:", job)
msg = """
Information of user %s:
-------------------------
Name: %s
Age : %d
Job : %s
---------End-------------
""" % (name, name, age, job)
print(msg)

%d:整数

%f:浮点数

%s:字符串

(5)隐藏输入密码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: WangHuafeng
import getpass
username = input("请输入用户名:")
password = input("请输入密码:")
print(username, password)

注意:Pycharm上输入密码时没有隐藏,Linux上可以隐藏。

(6)导入模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: WangHuafeng
import os
cmd_java = os.system("java -version")
print(cmd_java)

注意:导入模块或自己的python文件时不能加.py即:import Hello

(7)Linux上增加tab补全

新建文件tab.py

# python startup file
import sys
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind(‘tab: complete‘)
# history file
histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

保存到/usr/lib/python2.7/dist-packages目录中

python命令行中输入:import tab

即可tab查看命令提示。

Day1 初识Python

标签:

原文地址:http://www.cnblogs.com/icsnow/p/5598254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!