python基础篇-day1
Posted 唐胜伟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础篇-day1相关的知识,希望对你有一定的参考价值。
python基础篇
python是由C语言写的;
pass 占位符;
变量赋值的方法:
user,pass = ‘freddy‘,‘freddy123‘ 【变量名全是大写,表示是一个全量,最好不要更改;】
for循环语法小技巧:
for i in range(10): if i >=3:break 【在程序比较小的时候,可以使用次方法】
python格式化输出:
name = input("you input name:") age = int(input("you input age:")) job = input("you input job:") msg = """ Infomation is %s ------------ name = %s age = %d job = %s ----END----- """ %(name,name,age,job) print(msg)
使输入的密码为密文,在pycharm中不好使:
import getpass username = input("username:") password = getpass.getpass("password:") print(username,password)
python和linux交互:
>>> import os >>> os.system("dir") os.mkdir
【python和linux交互变量赋值方法:把输出的结果赋值给变量名cmd_res,先用popen把结果保存到内存中,然后再用read读出来:】
cmd_res = os.popen("df -h").read()
print ("cmd_res")
【python,寻找python执行脚本文件路径:】
>>> import sys
>>> print (sys.path)
判断用户输入的账号密码是否正确:
【方法一:】 user="freddy" password="123" username = input("you input user:") userpass = input("you input password:") if username == user: print("user ok.") if userpass == password: print("passwd ok.") else: print("pass error") else: print("error error.....") 【方法二:】 if user == username and password == userpass: print("Wecomto Login.") else: print("User Or Password error.")
猜数字小程序:
【条件: 1.用户一共有十次机会; 2.猜三次以后问用户是否要继续; 3.如果猜对了,直接退出。】 count = 0 for i in range(10): if count < 3: age = 18 freddyAge=int(input("You Input Age:")) count +=1 if freddyAge == age: print("ok") break elif freddyAge < age: print("tai di le.") else: print("tai gao le") else: retry=input("Input you (y/n)") if retry == ‘y‘: count = 0 else: exit()
列表操作:
【列表赋值:】 >>> name=["freddy",‘tang‘,‘21‘,‘88‘] 【取tang字符,取的是下标,下标从0开始:】 >>> name[1] ‘tang‘ 【取88数字:】 >>> name[-1] ‘88‘ 【顾首不顾尾:】 >>> name=["freddy",‘tang‘,‘21‘,‘88‘] >>> name[0:2] [‘freddy‘, ‘tang‘] 【取后三个值,取几个就输入 - 几:】 >>> name[-3:] [‘tang‘, ‘21‘, ‘88‘] 【多次取列表里面的数据:】 >>> name=["freddy",‘tang‘,‘21‘,‘88‘,234,234,324,32,4] >>> name[-6:] [‘88‘, 234, 234, 324, 32, 4] >>> name[-6:][2:4] [234, 324] >>> name[-6:][2:4][0] 234 【给列表中234,改成freddie:】 >>> name=["freddy",‘tang‘,‘21‘,‘88‘,234,234,324,32,4] >>> name[4]=‘freddie‘ 结果: >>> name [‘freddy‘, ‘tang‘, ‘21‘, ‘88‘, ‘freddie‘, 234, 324, 32, 4] 【给列表插入值[在”tang“后面插入"sheng"]】 >>> name [‘freddy‘, ‘tang‘, ‘21‘, ‘88‘, ‘freddie‘, 234, 324, 32, 4] >>> name.insert(2,‘sheng‘) >>> name [‘freddy‘, ‘tang‘, ‘sheng‘, ‘21‘, ‘88‘, ‘freddie‘, 234, 324, 32, 4] 【在列表末尾追加一个值:】 [‘freddy‘, ‘tang‘, ‘sheng‘, ‘21‘, ‘88‘, ‘freddie‘, 234, 324, 32, 4] >>> name.append(‘tail‘) >>> name [‘freddy‘, ‘tang‘, ‘sheng‘, ‘21‘, ‘88‘, ‘freddie‘, 234, 324, 32, 4, ‘tail‘] 【删除列表中指定的值:】 >>> name [‘freddy‘, ‘tang‘, ‘sheng‘, ‘21‘, ‘88‘, ‘freddie‘, 234, 324, 32, 4, ‘tail‘] >>> name.remove("freddy") >>> name [‘tang‘, ‘sheng‘, ‘21‘, ‘88‘, ‘freddie‘, 234, 324, 32, 4, ‘tail‘] 【删除下标5~9,也是顾首不顾尾】 >>> name [‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, ‘23‘, 234, 54, 234, 123, 523] >>> del name[5:9] >>> name [‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, ‘23‘, 523] >>> 【删除列表中的值,是根据下标来删除的:】 name=[‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘,23, 523,23,43,23,435] name.pop(0) print(name) 【以布长为2,打印列表:】 >>> name [‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, ‘23‘, 523] >>> name[::2] [‘GroupLong-tang‘, ‘freddie‘, ‘23‘] >>> 【统计列表中23元素的个数(注意:如果列表中,23被‘单引号’括起来,就不能被count所计算):】 name=[‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, 23, 523,23,43,23,435] if 23 in name: list_count=name.count(23) print("---%s--- 23 is/are in name" %list_count) 【把name列表中的所有23元素,全部替换成99999999:】 name=[‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, 23, 523,23,43,23,435] for i in range(name.count(23)): list_index=name.index(23) name[list_index]=9999999999 print(name) 【扩展列表:】 name=[‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, 23, 523,23,43,23,435] name2=[1,2,3,4,5] name.extend(name2) print(name) 【反转显示列表内容:】 name=[‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, 23, 523,23,43,23,435] name.reverse() print(name) 【列表排序(注意:字符串和数字不能混排):】 name=[‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘] name.sort() print(name) name=[ 23, 523,23,43,23,435] name.sort() print(name) 【copy列表:】 name=[‘GroupLong-tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘,23, 523,23,43,23,435] name1=name.copy() name[0]=‘TANG‘ print(name) print(name1) 【浅copy,二级列表的内容不会被重新copy:】 name=[‘tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘,[23, 523,23],43,23,435] name1=name.copy() name[0]=‘TANG‘ name[4][1]=11111111 name1[4][2]=22222222 print(name) print(name1) 【深浅copy】 import copy name=[‘tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘,[23, 523,23],43,23,435] #浅copy,不会copy二级列表里面的元素; name1=name.copy() # name1=copy.copy(name) #深copy,不管有几级,都会copy过去; name2=copy.deepcopy(name) #和原文件不一样; name[0]=‘TANG‘ name[4][1]=11111111 name1[4][2]=22222222 name2[4][0]=33333333 print(name) print(name1) print(name2) 结果: [‘TANG‘, ‘sheng‘, ‘freddie‘, ‘wei‘, [23, 11111111, 22222222], 43, 23, 435] [‘tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, [23, 11111111, 22222222], 43, 23, 435] [‘tang‘, ‘sheng‘, ‘freddie‘, ‘wei‘, [33333333, 523, 23], 43, 23, 435]
元组 :
count,index 【只有两个方法】
以上是关于python基础篇-day1的主要内容,如果未能解决你的问题,请参考以下文章