标签:python基础day1
(1)变量与赋值
name = "wanghuafeng"
age = 29
print(name, age)
a和b交换值
a = 3
b = 5
tmp = a
a = b
b = tmp
print(a, b)
变量要求:
a.显式
b.nums_of_words = 19
c.NumsOfWords = 19 #驼峰写法
d."-"在任何语言中都是减号,保留符号
e.数字不能开头,可以在中间和结尾
f.特殊字符不能有,!@¥%……&*()
g.不能有空格、只能是下划线、数字和字母,关键字不能作为变量
(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:字符串
代码中的注释
#注释一行最多不超过80个字符 """ 多行注释, 不用每行注释 """ ‘‘‘ 也可以采用这个注释 ‘‘‘
(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查看命令提示。
(8)if判断
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author: wanghuafeng user = ‘wanghuafeng‘ passwd = ‘123456‘ username = input("input your name:") password = input("input your password:") if user == username and passwd == password: print("Welcome to login...") else: print("用户名或密码错误...")
(9)猜年龄
#!/usr/bin/env python # -*- coding: utf-8 -*- # Author: wanghuafeng age = 22 counter = 0 for i in range(10): print("counter is :", counter) if counter < 3: guess_num = int(input("input your guess num: ")) if guess_num == age: print("Congratulations! You got it.") break elif guess_num > age: print("Think smaller!") else: print("Think Big...") else: continue_confirm = input("Do you want to continue because you are stupid:") if continue_confirm == ‘y‘: counter = 0 continue else: print("Bye.") break counter += 1
标签:python基础day1
原文地址:http://icsnow.blog.51cto.com/3302206/1791530