Giter Site home page Giter Site logo

huanghl365 / learnpython Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kennycaiguo/kenny-learnpython

0.0 1.0 0.0 45.53 MB

学习python编程

Python 94.30% HTML 0.01% JavaScript 0.03% CSS 0.03% Tcl 2.76% PowerShell 0.04% Batchfile 0.01% C 1.84% C++ 0.94% Objective-C 0.02% Fortran 0.01% Smarty 0.01%

learnpython's Introduction

LearnPython

学习python编程

python 递归

febonacci.py

def febonacci(i): if(i==0): return 0 if(i==1): return 1 return febonacci(i-1)+febonacci(i-2)
feb = [] for i in range(0,10): feb.append(febonacci(i)) print(feb)

factorial.py //计算一个数的阶乘如:54321.。。

def factorial(i): if(i==0): return 0 if(i==1): return 1 return i*factorial(i-1) i = 15 print("{}的阶乘={}".format(i,factorial(i)))

Python遍历文件夹,使用os.walk(文件夹路径)

import os

def WalkThruDir(dir): g = os.walk(dir) for path,dirlist,filelist in g: for file in filelist: print(os.path.join(path,file)) WalkThruDir("e:/pythonstudy")

python 分离奇数和偶数

nums = [1,2,3,4,5,11,12,13,14,15] odds = [] events = [] while(len(nums) > 0): num = nums.pop() if(num%2==0): events.append(num) else: odds.append(num)
print(odds,events)

python 变量列表

colors = ['red', 'green', 'blue', 'yellow', 'white', 'black']

for color in colors: print("color is :",color)

python格式化输出

实例1

for x in range(1, 11): print('{0:2d} {1:3d} {2:4d}'.format(x, xx, xx*x))

实例2

print('{}网址: "{}!"'.format('菜鸟教程', 'www.runoob.com')) 菜鸟教程网址: "www.runoob.com!"

Python发送电子邮件:

import smtplib from email.mime.text import MIMEText from email.header import Header

mail_host ="smtp.gmail.com" mail_user="[email protected]" mail_pass="你的账户密码" #这里要改成你的账户密码 port=465

sender = "[email protected]" receiver="[email protected]"

message=MIMEText("Python mail test",'plain','utf-8') subject="mail test" message['subject']=Header(subject,'utf-8')

try: smtpobj = smtplib.SMTP_SSL(mail_host,port) smtpobj.login(mail_user,mail_pass) smtpobj.sendmail(sender,receiver,message.as_string()) except smtplib.SMTPException: print("fail to send mail")

python实现只要用户输入的不是exit,就将用户的输入保存到一个叫"input.txt" 的文件中

file = open("input.txt", mode='a+') if file==None: //这个非常重要,因为python普通的打开文件方式,不会插件新文件,而以下这个选项只有当文件不存在的时候才创建,文件存在就报错,所以需要使用if作为判断 file=open("input.txt",mode='X') else: flag=True while flag: content = input("请输入内容:\n") print(content) if content!='exit': file.write(content+"\n") if content == 'exit': flag=False file.flush() file.close() exit(0)

python读取xml文件的内容并且解析1

from xml.etree import ElementTree as et ## 这个用来解析xml字符串 xml1=open("person.xml") lines=[] lines2=[] lines=xml1.readlines() lines=lines[1:] #这句话的作用是去除xml声明语句,因为后面的xml解析不需要 for x in lines: if x[len(x)-1]=='\n': lines2.append(x[0:len(x)-1]) #去除换行符 else: lines2.append(x) strlines = "".join(lines2) print(strlines) root = et.fromstring(strlines) print(root.tag) child1 =root.getitem(0) child2 =root.getitem(1) child3 =root.getitem(2)

print(child1,child2,child3)

python读取xml文件,并且删除每一个子节点的值:very good

import xml.etree.ElementTree as et

tree = et.ElementTree(file="persons.xml") #### 这个文件必须放在虚拟环境的env文件夹中

print(tree)

root = tree.getroot() ####获取跟节点

print(root)

print(root[0][0].text)

for child in root: # 遍历root的子节点 print(child.tag,"\nformat:name value") for childnode in child: ####遍历每一个节点的子节点 print(childnode.tag,childnode.text) print("================")

Python 把字典输出到excel文件

import pandas as pds

dict={ "Jack":{"chinese":80,"english":90,"physics":78,"math":77}, "Lili":{"chinese":70,"english":80,"physics":70,"math":67}, "Mike":{"chinese":85,"english":70,"physics":90,"math":87}, "Christy":{"chinese":60,"english":91,"physics":70,"math":70}, } df =pds.DataFrame(dict) df.to_excel('student.xlsx')

learnpython's People

Contributors

kennycaiguo avatar kennycai1975 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.