python基础知识学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础知识学习笔记相关的知识,希望对你有一定的参考价值。
字符编码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
注释:
单行注释: # 注释内容
多行注释: ‘‘‘ 注释内容 ‘‘‘
用户输入信息的保存并输出
3.X
user_input = input("your name:")
print("user name:",user_input)
2.X
user_input = raw_input("your name:")
print("user name:",user_input)
格式化输出:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Hudengzhi name = input("your name:") age = int(input("your age:") ) job = input("your job:") msg = ‘‘‘Infomation of user %s name : %s age : %d job : %s ‘‘‘% (name, name, age, job) print(msg)
常用模块初识
#1.密文输入
import getpass
username = input("")
password = getpass.getpass("password:")
print(username,password)
#2. os模块使用
import os
os.system("df")
os.mkdir("yourdir")
os.system("ls")
cmd_res = os.popen("df -h").read()
print(cmd_res)
#3.模块存放路径
import sys
print("sys.path")
/usr/lib/python2.7/dist-packages ##自己写的模块一般放在此目录
#4. if 语句
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Hudengzhi
user = "Hudengzhi"
passwd = "123456"
username = input("username:")
password = input("password:")
if username == user:
print("username is correct...")
if password == passwd:
print("Welcome login ...")
else:
print("password is invalid...")
else:
print("连用户名都没写对,滚出!")
#5 for 循环
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Hudengzhi
age = 22 for i in range(10): guess_num = int(input("age:") ) if guess_num == age: print("Congratulations , you got it") break #不再往后走,跳出整个loop elif guess_num > age: print("Think smaller") else: print("Think big")
本文出自 “python基础学习笔记” 博客,请务必保留此出处http://hudengzhi.blog.51cto.com/1319933/1859045
以上是关于python基础知识学习笔记的主要内容,如果未能解决你的问题,请参考以下文章