Day1 初识Python

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Day1 初识Python相关的知识,希望对你有一定的参考价值。

(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的主要内容,如果未能解决你的问题,请参考以下文章

Python初识-day1

Day1 Python笔记

Python 变量&对象初识

DAY1-初识python

Python day1

python基础学习day1