#+SETUPFILE: theme-bigblow.setup
* faq
** rename
>>> for quizNum in range(35):
quizFile=open('capitalsquiz%s.txt' %(quizNum +1), 'w')
#Python3+
name = input("who are you? ")
print("hello %s" % (name,))
* Installation
To check whether you OS is 32-bit or 64-bit
#+begin_src
uname -m
#+end_src
which may return:
=x86_64=
#+begin_src
~sudo apt-get install python3~
~sudo apt-get install idle3~
~sudo apt-get install python3-pip~
#+end_src
* Introduction
** intro
- python :: interpreted, high-level, general-purpose programming language
With python, you can automate simple tasks such as the following:
- Moving and renaming thousands of files and sorting them into folders
- Filling out online forms, no typing required
- Downloading files or copy text from a website whenever it updates
- Having your computer text you custom notifications
- Updating or formatting Excel spreadsheets
- Checking your email and sending out prewritten responses
(Python for Non-Programmers[b])
All programs use basic instructions as building blocks.
Here are a few of the most common ones, in English:
“Do this; then do that.”
“If this condition is true, perform this action; otherwise, do that action.”
“Do this action that number of times.”
“Keep doing that until this condition is true.”
** platforms
- IPython :: An enhanced Interactive Python
- anaconda
- python tutor -- visualize code
1. IDLE
2. terminal
[Math Processing Error]?′>>>a+b‘Doyouseethis,[Math Processing Error] idle3
2. declare a *string* variable that holds *the path to the text file*, =test.txt=
>>> strPath="/home/kaiming/Documents/Python/text/text.dat"
3. open the file using the =open()= function
>>> f=open(strPath)
4. Read the contents of the file using the =read()= function
>>> StrText=f.read()
5. Print out the contents of the file
>>> print(strText)
refer to [[https://docs.python.org/2/tutorial/inputoutput.html][input]] and out
** How to read and write multiple files?
Goal:
I want to write a program for this:
In a folder I have =n= number of files;
first read one file and perform some operation then store result in a separate file.
Then read 2nd file, perform operation again and save result in new 2nd file.
Do the same procedure for n number of files.
The program reads all files one by one and
stores results of each file separately.
solution:
#+BEGIN_SRC Python
import sys #import 'sys' module
#argv is your commandline arguments, argv[0] is your program name, so skip it
for n in sys.argv[1:]:
print(n) #print out the filename we are currently processing
input=open(n,"r")
output=open(n,+".out","w")
# do some processing
input.close()
output.close()
#+END_SRC
- sys :: system-specific parameters and functions
- module :: a file contains Python code
Before you use the functions in a module, you must import the module with an =import= statement
why python module?
Python module is used to group related functions, classes, and variables
for better code management and avoiding name clash
https://stackoverflow.com/questions/208120/how-to-read-and-write-multiple-files
* Glossary
- argument :: a value that is passed to a function
e.g. function-name(argument)
* notes - Python for Non-Programmers --book
[[https://automatetheboringstuff.com/][automate the boring stuff with python-practical programming for total beginners]]
- The programs in this book are written to run on *Python 3*
* Function
*built-in funcions* include the =print()=, =input()=, =len()=
To define a function
>> def function_name()
** 函数的参数
除了正常定义的必选参数外,还可以使用默认参数、可变参数和关键字参数
*** 位置参数
我们先写一个计算x2的函数:
def power(x):
return x * x
对于power(x)函数,参数x就是一个位置参数。
当我们调用power函数时,必须传入有且仅有的一个参数x:
>>> power(5)
25
>>> power(15)
225
现在,如果我们要计算x3怎么办?可以再定义一个power3函数,但是如果要计算x4、x5……怎么办?我们不可能定义无限多个函数。
你也许想到了,可以把power(x)修改为power(x, n),用来计算xn
def power(x, n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
对于这个修改后的power(x, n)函数,可以计算任意n次方:
>>> power(5, 2)
25
>>> power(5, 3)
125
修改后的power(x, n)函数有两个参数:x和n,这两个参数都是位置参数,调用函数时,传入的两个值按照位置顺序依次赋给参数x和n
** 递归函数
如果一个函数在内部调用自身,这个函数就是递归函数。
举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以看出:
fact(n) = n! = 1 x 2 x 3 x ... x (n-1) x n = (n-1)! x n = fact(n-1) x n
所以,fact(n)可以表示为n x fact(n-1),只有n=1时需要特殊处理。
于是,fact(n)用递归的方式写出来就是:
#+BEGIN_SRC Python
def fact(n):
if n==1:
return 1
return n * fact(n - 1)
#+END_SRC
如果我们计算fact(5),可以根据函数定义看到计算过程如下:
===> fact(5)
===> 5 * fact(4)
===> 5 * (4 * fact(3))
===> 5 * (4 * (3 * fact(2)))
===> 5 * (4 * (3 * (2 * fact(1))))
===> 5 * (4 * (3 * (2 * 1)))
===> 5 * (4 * (3 * 2))
===> 5 * (4 * 6)
===> 5 * 24
===> 120
** range()
Syntax:
range(start, end, step size)
e.g. ~range(0, 10, 2)~ will count from 0 to 8 by intervals of 2
#+BEGIN_SRC Python
>>> for i in range(0, 10, 2):
print(i)
0
2
4
6
8
#+END_SRC
* lists and tuples
- list :: a value that contains multiple values in an ordered suequence
+ typed with []
e.g. =['cat','bat','rat', 'element']=
What is list used for?
- tuple :: almost identical to the *list* data type, but immutable
+ typed with (),
- list value :: refers to the list itself, not the values inside the list value
- iterms :: values inside the list
*To insert a iterm after the item 1 in a list:*
>>> classmates=['Michael', 'Bob']
>>> classmates.insert=[1,'jack']
['Michael','Jack', 'Bob']
* courses
edx [[https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/references/][Intro to computer sicence and programming]]--Eric Grimson, MIT
* References
Introduction to Computation and Programming Using Python, Second Edition
https://docs.python.org/2/tutorial/index.html
https://www.python.org/about/gettingstarted/
** video
tutorials-giraffe academy
** Documentation
•Official Python 3 Documentation - "official"/technical explanation of what a particular function/operator does, examples of correct syntax, what the various libraries are, etc.
Textbooks/Tutorials
•Dive Into Python - another survey of Python syntax, datatypes, etc.
•Think Python by Allen Downey - a good general overview of the Python language. Includes exercises.
•The Official Python Tutorial - self-explanatory
•Learn Python the Hard Way - (note: for Python 2) another free online text
•Reserved Keywords in Python - don't use these as variable names
•PEP 8 - Style Guide for Python Code - learn what is good and bad style in Python
•CheckIO - learn Python by exploring a game world
•Invent with Python - develop your Python skills by making games or hacking ciphers
•Codecademy - (note: for Python 2) learn Python by building web apps and manipulating data; interactive tutorial sequence
•Python Tutor - interactive tutorial sequence of exercises
•Blog with tutorials - created by one of our community TAs
** Debugging
•Python Tutor - an excellent way to actually visualize how the interpreter actually reads and executes your code
•DiffChecker - compares two sets of text and shows you which lines are different
•Debugging in Python - steps you can take to try to debug your program
Software
•Python Tools for Visual Studio - Visual Studio plug-in enabling Python programming
Other Q&A
** More practice problems
•Python Challenge - a series of puzzles you can try to test your Python abilities
•Project Euler - additional programming challenges you can try once your Python knowledge becomes stronger; problems are sorted by increasing difficulty
•Coding Bat - problems you can solve within an online interpreter
•Codewars - improve your skills by training on real code challenges
** non-programmer
Interactive Courses
CheckiO is a gamified website containing programming tasks that can be solved in either Python 2 or 3.
Python on Codecademy (Python 2)
笨方法学 Python
http://learnpythonthehardway.org/book/
「廖雪峰的 Python 2.7 教程」:Home - 廖雪峰的官方网站Python 中文教程的翘楚,专为刚刚步入程序世界的小白打造。
The Hitchhiker’s Guide to Python
链接:https://www.zhihu.com/question/29138020/answer/72193349
** chinese
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431658427513eef3d9dd9f7c48599116735806328e81000
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
2020-01-14 test