搜档网
当前位置:搜档网 › python 基础练习题

python 基础练习题

python 基础练习题
python 基础练习题

Advanced computation linguistics

1. Collect the most frequent words in 5 genres of Brown Corpus:

news, adventure, hobbies, science_fiction, romance

To collect most frequent words from the given genres we can follow the following steps:

>>> import nltk

>>> from nltk.corpus import brown

>>> brown.categories()

['adventure', 'belles_lettres', 'editorial', 'fiction', 'government', 'hobbies',

'humor', 'learned', 'lore', 'mystery', 'news', 'religion', 'reviews', 'romance'

, 'science_fiction']

>>> news_text = brown.words(categories=['news','adventure','hobbies','science_fi

ction','romance'])

>>> from nltk.probability import FreqDist

>>> fdist=FreqDist([w.lower() for w in news_text])

>>> voca=fdist.keys()

>>> voca[:50]

['the', ',', '.', 'and', 'of', 'to', 'a', 'in', 'he', "''", '``', 'was', 'for',

'that', 'it', 'his', 'on', 'with', 'i', 'is', 'at', 'had', '?', 'as', 'be', 'you

', ';', 'her', 'but', 'she', 'this', 'from', 'by', '--', 'have', 'they', 'said',

'not', 'are', 'him', 'or', 'an', 'one', 'all', 'were', 'would', 'there', '!', '

out', 'will']

>>> voca1=fdist.items()

>>> voca1[:50]

[('the', 18635), (',', 17215), ('.', 16062), ('and', 8269), ('of', 8131), ('to',

7125), ('a', 7039), ('in', 5549), ('he', 3380), ("''", 3237), ('``', 3237), ('w

as', 3100), ('for', 2725), ('that', 2631), ('it', 2595), ('his', 2237), ('on', 2

162), ('with', 2157), ('i', 2034), ('is', 2014), ('at', 1817), ('had', 1797), ('

?', 1776), ('as', 1725), ('be', 1610), ('you', 1600), (';', 1394), ('her', 1368)

, ('but', 1296), ('she', 1270), ('this', 1248), ('from', 1174), ('by', 1157), ('

--', 1151), ('have', 1099), ('they', 1093), ('said', 1081), ('not', 1051), ('are

', 1019), ('him', 955), ('or', 950), ('an', 911), ('one', 903), ('all', 894), ('

were', 882), ('would', 850), ('there', 807), ('!', 802), ('out', 781), ('will',

775)]

This means that the frequency of word “the” is more than others.

2. Exclude or filter out all words that have a frequency lower than 15 occurrencies. (hint using conditional frequency distribution)

By adding functionalities on the first task of collecting words based on their frequency of

occurrences, we can filter words which has frequency occurrence of >=15.

>>> filteredText= filter(lambda word: fdist[word]>=15,fdist.keys())

>>> voca=fdist.keys()

>>> filteredText[:50] /*first 50 words*/

['the', ',', '.', 'and', 'of', 'to', 'a', 'in', 'he', "''", '``', 'was', 'for',

'that', 'it', 'his', 'on', 'with', 'i', 'is', 'at', 'had', '?', 'as', 'be', 'you

', ';', 'her', 'but', 'she', 'this', 'from', 'by', '--', 'have', 'they', 'said',

'not', 'are', 'him', 'or', 'an', 'one', 'all', 'were', 'would', 'there', '!', '

out', 'will']

>>> filteredText[-50:] /*last 50 words*/

['musical', 'naked', 'names', 'oct.', 'offers', 'orders', 'organizations', 'para

de', 'permit', 'pittsburgh', 'prison', 'professor', 'properly', 'regarded', 'rel

ease', 'republicans', 'responsible', 'retirement', 'sake', 'secrets', 'senior',

'sharply', 'shipping', 'sir', 'sister', 'sit', 'sought', 'stairs', 'starts', 'st

yle', 'surely', 'symphony', 'tappet', "they'd", 'tied', 'tommy', 'tournament', '

understanding', 'urged', 'vice', 'views', 'village', 'vital', 'waddell', 'wagner

', 'walter', 'waste', "we'd", 'wearing', 'winning']

3. Then exclude or filter out all stopwords from the lists you have created.(hint using conditional frequency distribution)

To filter the stop words we have to define tiny function using the word net library for 'english' language.

>>> from nltk.corpus import stopwords

>>> stopwords.words('english')

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yo

urs', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'he

rs', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'thems

elves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am',

'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having

', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'be

cause', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'again

st', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below'

, 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again'

, 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'al

l', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no',

'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'ca

n', 'will', 'just', 'don', 'should', 'now']

>>> def content_fraction(text):

... stopwords= nltk.corpus.stopwords.words('english')

... content = [w for w in text if w.lower() not in stopwords]

... return len(content) / len(text)

...

>>> content_fraction(nltk.corpus.reuters.words())

0.65997695393285261

>>> filterdText = filterStopword(freqDist)

>>> filterdText[:50]

[',', '.', "''", '``', '?', ';', '--', 'said', 'would', 'one', '!', 'could', '(', ')', ':', 'time', 'like', ' back',

'two', 'first', 'man','made', 'Mrs.', 'new', 'get', 'way', 'last', 'long', 'much', 'even', 'years', '

good', 'little', 'also', 'Mr.', 'see','right', 'make', 'got', 'home', 'many', 'never', 'work', 'know',

'day' , 'around', 'year', 'may', 'came', 'still']

>>> freqDist[:50]

[',', 'the', '.', 'of', 'and', 'to', 'a', 'in', "''", '``', 'was', 'for', 'that', 'he', 'on', 'with', 'his', 'I', 'it',

'is', 'The', 'had', '?','at', 'as', 'be', ';', 'you', 'her', 'He', '--', 'from', 'by', 'said', 'h ave', 'not',

'are', 'this', 'him', 'or', 'were', 'an', 'but','would', 'she', 'they', 'one', '!', 'all', 'out ']

From the result in filterdText words like 'the', 'it', 'is' and so on does not exist

compared to the same number of output with stop words.

>>> len(freqDist)

2341

>>> len(filterdText)

2153

We can further check that how many stop-words have been removed from the freqDist15 using len( ) function.

4. Create a new list of lemmas or roots by normalizing all words by stemming

for create the normalized list of lemmas we apply the Porter Stemmer nltk functionality.

>>> file = open('filterdText.txt')

>>> text = file.read()

>>> textTokens = nltk.word_tokenize(text)

Now we do stemming

>>> p = nltk.PorterStemmer ( )

>>> rootStemming = [p.stem(t) for t in textTokens]

>>> textTokens[:100]

['!', '&', "'", "''", "'em", '(', ')', ',', '--', '.', '1', '10', '100', '11', '12', '13', '14', '15', '16', '17', '18', '1958', '1959','1960', '1961', '2', '20', '200', '22', '25', '3', '30', '4', '5', '50', '6', '60 ', '7', '8', '9', ':', ';', '?', 'A.', 'Actually','Af', 'Ah', 'Aj', 'Alexander', 'Also', 'Although', 'Americ a', 'American', 'Americans',

'Among', 'Angeles','Anne', 'Anniston', 'Another', 'April', 'Association', 'Augu st', 'Austin', 'Avenue', 'B', "B'dikkat", 'B.','Barton', 'Beach', 'Belgians', 'Besides', 'Bill', 'Billy', 'B lue', 'Board', 'Bob',

'Bobbie', 'Boston', 'Brannon','British', 'C.', 'Cady', 'California', 'Catholic', 'Cath y', 'Center', 'Central', 'Charles', 'Charlie', 'Chicago','Christian', 'Church', 'City', 'Class', 'Clayton', 'Club', 'Co.', 'Coast',

'Cobb', 'College']

This function can display sorted non normalized sample outputs for comparison

>>> rootStemming[:100]

['!', '&', "'", "''", "'em", '(', ')', ',', '--', '.', '1', '10', '100', '11', '12', '13', '14', '15', '16', '17', '18', '1958', '1959','1960', '1961', '2', '20', '200', '22', '25', '3', '30', '4', '5', '50', '6', '60 ', '7', '8', '9', ':', ';', '?', 'A.',

'Actual', 'Af','Ah', 'Aj', 'Alexand', 'Also', 'Although', 'America', 'American', 'American', 'Among',

'Angel', 'Ann','Anniston', 'Anoth', 'April', 'Associ', 'August', 'Austin','Avenu', 'B', "B'dikkat", 'B.',

'Barton', 'Beach','Belgian', 'Besid', 'Bill', 'Billi', 'Blue', 'Board', 'Bob ', 'Bobbi', 'Boston', 'Brannon',

'British', 'C.', 'Cadi','California', 'Cathol', 'Cathi','Center','Central','Charl','Charli','Chicago','Christian', 'Church', 'Citi','Class', 'Clayton', 'Club', 'Co.', 'Coast', ' Cobb', 'Colleg']

This can sorted stemmed sample output for comparison

5. Create a new list of lemmas or roots by normalizing all words by lemmatization

After importing the file we need to lemmatize, which is the same step as the previous one:

and using the same rawText

>>> wnl = nltk.WordNetLemmatizer()

>>> rootLemmatize = [wnl.lemmatize(t) for t in textTokens]

>>> rootLemmatize[:100] / *the first 100 sorted lemmatized lemmas for comparison*/

['!', '&', "'", "''", "'em", '(', ')', ',', '--', '.', '1', '10', '100', '11', '12', '13', '14', '15', '16', '17', '18', '1958', '1959','1960', '1961', '2', '20', '200', '22', '25', '3', '30', '4', '5', '50', '6', '60 ', '7', '8', '9', ':', ';', '?', 'A.', 'Actually','Af', 'Ah', 'Aj', 'Alexander', 'Also', 'Although', 'Americ a', 'American', 'Americans',

'Among', 'Angeles','Anne', 'Anniston', 'Another', 'April', 'Association', 'Augu st', 'Austin', 'Avenue', 'B', "B'dikkat", 'B.','Barton', 'Beach', 'Belgians', 'Besides', 'Bill', 'Billy', 'B lue', 'Board', 'Bob',

'Bobbie', 'Boston', 'Brannon','British', 'C.', 'Cady', 'California', 'Catholic', 'Cath y', 'Center', 'Central', 'Charles', 'Charlie', 'Chicago','Christian', 'Church', 'City', 'Class', 'Clayton', 'Club','Co.','Coast','Cobb', 'College']

We end this task by writing the out put of the lemmatized lemmas or root

words to the file '(rootLemmatize.txt').

6. Use the most frequent lemmas to find semantic similarities using WordNet.

To find synsets with related meanings we have to traverse the WordNet network. knowing which word is semantically related is useful for indexing a collection of texts. For example a search for a general term like 'England' will match for specific terms like 'UK'.

Top 100 frequent lemmas:

>>> file = open('filterdText.txt') /*from 'filterdText' we get the words*/

>>> tmp = file.read()

>>> from nltk.tokenize import RegexpTokenizer /*remove punctuations*/

>>>

>>> tokenizer = RegexpTokenizer(r'\w+')

>>> textSimilarity = tokenizer.tokenize(tmp)

>>> freqDistSimilarity = FreqDist([w.lower() for w in textSimilarity]) /* extract the first 100 frequent lemmas to new list*/

>>> for word in textSimilarity:

... freqDistSimilarity.inc(word)

>>> tmpFDS = freqDistSimilarity.keys()[:100] /* first 100 most frequent lemmas*/

>>> freqDistSimilarity.items()[:50]

[('s', 64), ('t', 60), ('re', 24), ('d', 22), ('you', 20), ('ll', 18), ('m', 14), ('he', 12), ('let', 12), ('man', 10), ('p',10), ('we', 10), ('I', 8), ('i', 8), ('ve', 8), ('won', 8), ('year', 8), ('B', 6), ('a', 6), ('actually', 6), ('also', 6),('although', 6), ('among', 6), ('another', 6), ('association', 6), ('b', 6), ('beach', 6), ('bill', 6),('blue', 6),('board', 6), ('center', 6), ('central', 6), ('church', 6), (' city', 6), ('class', 6), ('club', 6), ('college', 6), ('come', 6), ('committee',6) ,('council',6),('county',6),('court',6),('day',6),('department',6),('district', 6), ('don', 6), ('earth', 6), ('education', 6), ('even', 6), ('every', 6)]

>>> def pathSimilarity(word1,word2, s=wnet.path_similarity): /*path similarity between two words*/

... synSets1= wnet.synsets(word1)

... synSets2= wnet.synsets(word2)

... pointSimilarity = []

... for synSet1 in synSets1:

... for synSet2 in synSets2:

... pointSimilarity.append(s(synSet1,synSet2))

... if len(pointSimilarity)==0:

... return 0

... else:

... return max(pointSimilarity)

>>> tmpFDS[30:35] /*arbitrary path similarity test for 5 lemmas*/

['center', 'central', 'church', 'city', 'class']

>>> for word1 in tmpFDS[30:35]:

... for word2 in tmpFDS[30:35]:

... print word1+' == '+word2+' -->', pathSimilarity(word1,word2)

center == center --> 1.0

center == central --> 0.25

center == church --> 0.25

center == city --> 0.166

center == class --> 0.5

central == center --> 0.2

central == central --> 1.0

central == church --> 0.083

central == city --> 0.111

central == class --> 0.083

church == center --> 0.25

church == central --> 0.2

church == church --> 1.0

church == city --> 0.166

church == class --> 0.2

city == center --> 0.166

city == central --> 0.111

city == church --> 0.166

city == city --> 1.0

city == class --> 0.25

class == center --> 0.5

class == central --> 0.166

class == church --> 0.2

class == city --> 0.25

class == class --> 1.0

Because of the different sizes of synset for the lemmas we can get different path similarity . An independent path similarity test on the synonym sets also proves the same hypothesis.

We continue the analysis with the rest of the lemmas by calling the pathSimilarity( ) function with the provided arguments. From above results we can say that the root word 'class' and

'center' has a better semantic similarity.

Python基础知识笔试

Python基础知识笔试 单选题(2.5分*20题) 1. 下列哪个表达式在Python中是非法的?B A. x = y = z = 1 B. x = (y = z + 1) C. x, y = y, x D. x += y 2. python my.py v1 v2 命令运行脚本,通过from sys import argv如何获得v2的参数值? C A. argv[0] B. argv[1] C. argv[2] D. argv[3] 3. 如何解释下面的执行结果? B

print 1.2 - 1.0 == 0.2 False A. Python的实现有错误 B. 浮点数无法精确表示 C. 布尔运算不能用于浮点数比较 D. Python将非0数视为False 4. 下列代码执行结果是什么? D x = 1 def change(a): x += 1 print x change(x) A. 1 B. 2 C. 3

D. 报错 5. 下列哪种类型是Python的映射类型?D A. str B. list C. tuple D. dict 6. 下述字符串格式化语确的是?D A. 'GNU's Not %d %%' % 'UNIX' B. 'GNU\'s Not %d %%' % 'UNIX' C. 'GNU's Not %s %%' % 'UNIX' D. 'GNU\'s Not %s %%' % 'UNIX' 7. 在Python 2.7中,下列哪种是Unicode编码的书写方式?C A. a = ‘中文’ B. a = r‘中文’ C. a = u’中文’

python基础100练习题

题目有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?程序分析遍历全部可能,把有重复的剃掉。 total=0 for i in range(1,5): for j in range(1,5): for k in range(1,5): if ((i!=j)and(j!=k)and(k!=i)): print(i,j,k) total+=1 print(total) 12345678 简便方法用itertools中的permutations即可。 import itertools sum2=0 a=[1,2,3,4] for i in itertools.permutations(a,3): print(i) sum2+=1 print(sum2) 12345678 实例002:“个税计算” 题目企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?程序分析分区间计算即可。 profit=int(input('Show me the money: ')) bonus=0 thresholds=[100000,100000,200000,200000,400000] rates=[0.1,0.075,0.05,0.03,0.015,0.01] for i in range(len(thresholds)): if profit<=thresholds[i]: bonus+=profit*rates[i] profit=0 break else: bonus+=thresholds[i]*rates[i] profit-=thresholds[i] bonus+=profit*rates[-1] print(bonus) 14

运维必备Python基础入门到精通

运维必备Python基础入门到精通 视频课程汇总

Python函数中文手册 函数列表 1、取绝对值 abs(x) Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned. 如果你不知道绝对值什么意思,那就要补一下小学数学了! 基本用法 2 all(iterable)

Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to: 3 any(iterable) Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to: 4 basestring() This abstract type is the superclass for str and unicode. It cannot be called or instantiated, but it can be used to test whether an object is an instance of str or unicode. isinstance(obj, basestring) is equivalent to isinstance(obj, (str, unicode)). 是字符串和字符编码的超类,是抽象类型。不能被调用或者实例化。可以用来判断实例是否为字符串或者字符编码。 5、二进制转换 bin(x) Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer. 转换成二进制表达

python基础习题集

python基础习题集

python基础习题集 1.执行Python 脚本的两种方式解释器:python 脚本名.py直接运行:linux下给全部权限,然后再终端中输入./脚本名.py 2.简述位、字节的关系8位=1字节 3.简述ascii、unicode、utf--‐8、gbk 的关系Ascii是最早的一套编码,后来国人扩展了Ascii表,制出了CB2312编码表,后来又扩展到了GBK编码表。后来又有了GB18030。后来国际化标准组织为了规范化编码标准,研究出了unicode编码。经过一系列进化,发展到了可变长的utf-8编码 4.请写出“李杰”分别用utf--‐8 和gbk 编码所占的位数Utf-8:6位gbk:4位 5.Pyhton 单行注释和多行注释分别用什么?单行注释:#注释的内容多行注释:'''注释的内容''' 6.声明变量注意事项有那些?1、不能以数字开头2、只能是字母、数字或下划线的任意组合,但不能有特殊字符3、关键字通常不能声明为变量 7.如何查看变量在内存中的地址?Id(变量名) 8.执行Python 程序时,自动生成的.pyc 文件的作用是什么?pyc 是由py文件经过编译后二进制文件,使用pyc的加载速度快,而且跨平台。9、写代码 a.实现用户输入用户名和密码,当用户名为seven 且密码为123 时,显示登陆成功,否则登陆失败!Name =input(“Please input your name !”)Passwd =

int(input(“Please input you password !”))if Name == “seven”and Passwd ==123: print(“Welcome!”)else: print(“The name or password is wrong!”)b.实现用户输入用户名和密码,当用户名为seven 且密码为123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次for i in range(4): Name =input(“Please input your name !”) Passwd = int(input(“Please input you password !”)) if Name == “seven”and Passwd ==123: print(“Welcome!”) break else: print(“Please input agin!”)c.实现用户输入用户名和密码,当用户名为seven 或alex 且密码为123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次for i in range(4): Name =input(“Please input your name !”) Passwd = int(input(“Please input you password !”)) if (Name == “seven”or Name == “alex”)and Passwd ==123: Print(“Welcome!”) break else: print(“Please input agin!!”) 10、写代码a. 使用while 循环实现输出2 --‐3 + 4 --‐5 + 6 ... + 100 的和i=2t=0While i100: if i%2 == 1: t -= i else: t += i i += 1 print(t)b. 使用for 循环和range 实现输出1 --‐ 2 + 3 --‐ 4 + 5 --‐ 6 ... + 99 的和t=0for i in range(1,100): if i%2 == 1: t += i else: t -= i print(t)c. 使用while 循环实现输出1,2,3,4,5,7,8,9,11,12i=1while i 12: if i == 6 or i ==10: pass else: print(i) i += 1 d. 使用

python入门免费教程分享

python入门免费教程分享 通过Python入门爬虫比较简单易学,不需要在一开始掌握太多太基础太底层的知识就能很快上手,而且很快就能做出成果,非常适合小白一开始想做出点看得见的东西的成就感。你在学习Python?Python入门免费教程分享给你:python全新基础视频教程 https://https://www.sodocs.net/doc/1516097264.html,/s/1i5kvG5f python课程教学高手晋级视频h ttps://https://www.sodocs.net/doc/1516097264.html,/s/1htJW4KG python高级视频教程https://https://www.sodocs.net/doc/1516097264.html,/s/1nvf3NOt 那么,你是否清楚Python工程师在企业里面的定位是什么?主要有四个重要的定位:验证算法、快速开发、测试运维、数据分析。 1、验证算法:就是对公司一些常见设计算法或者公式的验证,公式代码化。 2、快速开发:这个大家应该都比较熟悉,快速开发,就是用成熟框架,更少的代码来开发网站; 3、测试运维:做运维同学应该清楚,在Linux运维工作中日常操作涵盖了监控,部署,网络配置,日志分析,安全检测等等许许多多的方面,无所不包。python可以写很多的脚本,把“操作”这个行为做到极致。

与此同时,python在服务器管理工具上非常丰富,配置管理(saltstack) 批量执行( fabric, saltstack) 监控(Zenoss, nagios 插件) 虚拟化管理 ( python-libvirt) 进程管理(supervisor) 云计算(openstack) ...... 还有大部分系统C库都有python绑定。 4、数据分析:Python有三大神器:numpy,scipy,matplotlib,其中numpy很多底层使用C语言实现的,所以速度很快,用它参加各种数学建模大赛,完全可以替代r语言和MATLAB。spark,Hadoop都开了Python的接口,所以使用Python做大数据的mapreduce也非常简单,加上py对数据库支持都很好,或者类似sqlalchemy的orm也非常强大好用。 了解完Python工程师在企业里面的定位,大家或许还需要找一家培训中心进行学习。 1.权威资深师资阵容,Python业内极具责任心、懂教学、拥有超强技术、有大型项目经验实战派讲师授课,由业内知名专家及企业技术骨干组成; 2.自主研发QFTS教学系统,拥有自主知识产权的Python开发培训课程体

Python语言基础 B卷

西安外国语大学考试试题(B)编号:3100101 2019-2020学年第2 学期院(部):经济金融学院专业:CFA、信管、金融、经济、国贸课程:Python语言基础年级:2019级命题时间:2020.06.12 命题教师:高妮 以上栏目由命题教师填写,下栏由考生填写 姓名:学号:班级: 一、综合题 已知某只股票2020年1月前半个月的开盘价和收盘价数据,其数据结构如表1所示。建一个Python脚本,脚本命名为“专业班级+姓名.py”,例如:"经济1901李艺茁.py",完成以下功能。 表1 某只股票2020年1月前半个月交易数据 日期开盘价(元)收盘价(元) 2020-01-01 12.3212.37 2020-01-02 12.2712.34 2020-01-03 12.2512.32 2020-01-06 12.2612.29 2020-01-07 12.2912.24 2020-01-08 12.2112.28 2020-01-09 12.4612.2 2020-01-10 12.4112.42 2020-01-13 12.412.41 2020-01-14 12.3612.4 1.将日期作为“键”,收盘价作为“值”存为一个字典。 2.假设1月15日的收盘价为12.5,将该数据加入字典。 3.假设现在是1月12日,使用datetime模块查询四天前的收盘价。 4.将1月13日的收盘价修改为12.34。 5.假设有一个交易策略,如果当期价格比前一期价格高,则买进,第二期卖出。初始 资产为10000元,用50%的现金买入股票,买入股票份额为整数。要求:产生一个 持有股票份额的字典对象。 6.利用NumPy模块获取需要买进的日期及当天的股价。 7.利用Matplotlib模块绘制某只股票2020年1月前半个月的两种价格(开盘价和收 盘价)曲线在同一图中,并添加必要的标题和坐标轴说明。 8.必须写出交易策略完整的设计思路。 9.必须写出绘图的基本流程。 10.必须给出程序代码以及所有功能正确运行的截图证明。 第1页/共1页

python 基础练习题

Advanced computation linguistics 1. Collect the most frequent words in 5 genres of Brown Corpus: news, adventure, hobbies, science_fiction, romance To collect most frequent words from the given genres we can follow the following steps: >>> import nltk >>> from nltk.corpus import brown >>> brown.categories() ['adventure', 'belles_lettres', 'editorial', 'fiction', 'government', 'hobbies', 'humor', 'learned', 'lore', 'mystery', 'news', 'religion', 'reviews', 'romance' , 'science_fiction'] >>> news_text = brown.words(categories=['news','adventure','hobbies','science_fi ction','romance']) >>> from nltk.probability import FreqDist >>> fdist=FreqDist([w.lower() for w in news_text]) >>> voca=fdist.keys() >>> voca[:50] ['the', ',', '.', 'and', 'of', 'to', 'a', 'in', 'he', "''", '``', 'was', 'for', 'that', 'it', 'his', 'on', 'with', 'i', 'is', 'at', 'had', '?', 'as', 'be', 'you ', ';', 'her', 'but', 'she', 'this', 'from', 'by', '--', 'have', 'they', 'said', 'not', 'are', 'him', 'or', 'an', 'one', 'all', 'were', 'would', 'there', '!', ' out', 'will'] >>> voca1=fdist.items() >>> voca1[:50] [('the', 18635), (',', 17215), ('.', 16062), ('and', 8269), ('of', 8131), ('to', 7125), ('a', 7039), ('in', 5549), ('he', 3380), ("''", 3237), ('``', 3237), ('w as', 3100), ('for', 2725), ('that', 2631), ('it', 2595), ('his', 2237), ('on', 2 162), ('with', 2157), ('i', 2034), ('is', 2014), ('at', 1817), ('had', 1797), (' ?', 1776), ('as', 1725), ('be', 1610), ('you', 1600), (';', 1394), ('her', 1368) , ('but', 1296), ('she', 1270), ('this', 1248), ('from', 1174), ('by', 1157), (' --', 1151), ('have', 1099), ('they', 1093), ('said', 1081), ('not', 1051), ('are ', 1019), ('him', 955), ('or', 950), ('an', 911), ('one', 903), ('all', 894), (' were', 882), ('would', 850), ('there', 807), ('!', 802), ('out', 781), ('will', 775)] This means that the frequency of word “the” is more than others. 2. Exclude or filter out all words that have a frequency lower than 15 occurrencies. (hint using conditional frequency distribution) By adding functionalities on the first task of collecting words based on their frequency of

python入门基础教程必学的11个知识点

python入门基础教程必学的11个知识点 Python被誉为全世界高效的编程语言,同时也被称作是“胶水语言”,那它为何能如此受欢迎,下面我们就来说说Python入门学习的必备11个知识点,也就是它为何能够如此受欢迎的原因。 1、Python适用于哪些应用场景? 这个没有固定答案,很多人都说Python不适合开发GUI的程序,但Python 自己的IDE——IDEL和第三方的IDE——Eric就是Python写的。 目前看到的更多的人是拿来写Web,使用如Django、web.py框架,没记错Flask也是。 也有一个情况用的比较多,用Python当胶水,与各种语言结合,共同完成某软件功能,注意观察也许你会发现在安装一些软件的时候会有Python的身影。 我个人还拿Python模拟过端口转发和DNS服务等,所以真的是要看真么用,而不是能怎么用。

另外大数据分析Python也是比较适合的,从载入到分析,再到保存结果等,Python有一整套的模块应对。 2、Python能够胜任大数据吗? Python很适合做大数据相关的分析,内置的C编译的模块能应对常见的操作,个别极端的算法建议用C重写相关模块。 Python本身的特点更多的是高效率的开发和简单的维护,速度交给C去吧,更多的问题其实出自写代码的人没有更好的使用,而不是效率不够高。比如排序,本来Python有非常高效的内置C编译的模块,却非要自己写算法,这样的结果不慢都是奇怪的。 另外还要看需求是CPU密集型,还是IO密集型,如果是CPU密集型建议这部分操作由C实现,IO密集型的效率不会因为Python而有多少改变。 C的效率是高,但框架搭起来也费劲,所以还是结合着来吧,也因此,Python 被称为胶水语言。 3、Python是否可以完全代替Shell? 完全可以,Shell的功能Python均可实现,而且代码量更少、结构更优、可阅读性更好,而Python可实现的功能Shell却不一定能,如运维中会用到的用于网络通信的Socket模块、用于WEB的Django框架、用于性能采集的psutil 模块等,而且Shell对操作系统的命令依赖性较强,Python可在更大程度上规避。 在一个Shell的IDE是个很大的问题,虽然Python的原生IDE不怎么样,但第三方的IDE还是功能十分强大的,虽然不能和微软的Virtual Studio相媲美,但也是能完全满足Python的开发需求的。

python练习题

Python 随堂练习 说明: 1.作业分为“基础要求”和“进阶要求” 2.作业首先要在本地完成,然后可以设计在蓝鲸应用中,以 3.交互的方式完成作业,可创意发挥。如:第8题,点击运行后,即可生存图片。若无法 在蓝鲸APP中实习,请在作业中附上代码以及执行结果截图。 4.13小题有条件的同学/协会可以试试。问答题,思考后,再运行。 5.前端设计方面,可参考bootstrap中的一些控件。 一、基础要求 1.print repr(u'中国') 的运行结果是什么? 2.什么是lambda函数?并列举一个使用lambda函数的例子 3.Excel操作 将 { "1":["张三",150,120,100], "2":["李四",90,99,95], "3":["王五",60,66,68] } 写入excel如下所示: 4.简述对Python装饰器的理解,写一个简单的装饰器。 5.生成100个随机数,保存到Redis非关系型数据库中 6.写结果,说明原因 if 1 in [1,0] == True: print …a? Else: Print …b? 7.用Python写一个程序,拉取SVN上的某一个文件,修改后并提交该文件。(请与蓝鲸给开发者的SVN结合起来) 8.用Python画出y=x3的散点图

9.用Python爬取知乎热门帖的标题,并存储到MySQL中(涉及django的model知识点) 二、进阶要求 10.Python 中数组套字典的排序(用lambda实现) dict = [ {'id':'4','name':'b'}, {'id':'6','name':'c'}, {'id':'3','name':'a'}, {'id':'1','name':'g'}, {'id':'8','name':'f'} ] 排序后:[{'id': '1', 'name': 'g'}, {'id': '3', 'name': 'a'}, {'id': '4', 'name': 'b'}, {'id': '6', 'name': 'c'}, {'id': '8', 'name': 'f'}] 11.利用python计算文件MD5值 (从前台上传一个文件,后台计算MD5值后,返给前端) 12.密码加密小工具 (对于部分喜欢将自己密码存在邮箱、网盘等容易被盗的朋友,可以自己记住一个唯一的密钥,通过这个小程序和密钥产生一串加密密文再存储,减少密码被盗几率。提示:Crypto 库 a.输入自己的秘钥:123456, b.选择是: encrypt 或者decrypt, c. 输出:加密后的密文,或者解密后的明文)

python入门基础教程网盘下载

python入门基础教程网盘下载 不知你是否听说,Python是面向企业和第一流项目的、基于生产的语言,而且它几乎可以完成任何事情。既能创建一个树莓派应用,又能用Python来写桌面程序的脚本,还能通过Python来配置服务器。也许你刚想学Python,也许你观望了很久,但小编觉得这篇python入门基础教程可以帮到 你。 千锋Python基础教程:https://www.sodocs.net/doc/1516097264.html,/s/1qYTZiNE Python课程教学高手晋级视频总目录: https://www.sodocs.net/doc/1516097264.html,/s/1hrXwY8k Python课程windows知识点:https://www.sodocs.net/doc/1516097264.html,/s/1kVcaH3x Python课程linux知识点:https://www.sodocs.net/doc/1516097264.html,/s/1i4VZh5b Python课程web知识点:https://www.sodocs.net/doc/1516097264.html,/s/1jIMdU2i Python课程机器学习:https://www.sodocs.net/doc/1516097264.html,/s/1o8qNB8Q Python课程-树莓派设备:https://www.sodocs.net/doc/1516097264.html,/s/1slFee2T 对初学者来说,Python是很好的语言。许多新开发人员都在用它,经验丰富的开发人员也并没有放弃它。因为使用Python还有许多可做的事情。

Python是一门通用的语言,被设计得极易读写。此外,由于它被认为是真正通用的、可满足多种开发需求的语言,所以也为程序员提供了各式各样的选择。Python可以被用作系统操作、Web开发、服务器和管理工具、科学建模等。 千锋教育人工智能+Python全栈是真正的Python全栈开发,包含Python项目,爬虫、服务集群、网站后台、微信公众号开发,Python机器学习与数据挖掘,数据分析框架与实战,Python物联网树莓派的开发等。 千锋Python培训班的讲师,可以说是业界难以超越的讲师天团。尹老师,刘老师,杨老师均是清华大学毕业的高材生,精通多门编程语言,拥有丰富的开发经验,多年IT名企工作经验以及丰富的项目实战经验。 抓紧机会参加千锋教育人工智能+Python全栈课程吧。免费的,真正不花一分钱。千万不要错过!

千锋教育Python入门教程

千锋教育Python入门教程 有人说,看着Python简单易学,想尝试自学,打算找些入门教程自学Python,不知道哪的视频好。现在,你可来对地方了,接下来附上千锋教育Python入门教程的链接。 千锋Python基础教程:https://www.sodocs.net/doc/1516097264.html,/s/1qYTZiNE Python课程教学高手晋级视频总目录: https://www.sodocs.net/doc/1516097264.html,/s/1hrXwY8k Python课程windows知识点:https://www.sodocs.net/doc/1516097264.html,/s/1kVcaH3x Python课程linux知识点:https://www.sodocs.net/doc/1516097264.html,/s/1i4VZh5b Python课程web知识点:https://www.sodocs.net/doc/1516097264.html,/s/1jIMdU2i Python课程机器学习:https://www.sodocs.net/doc/1516097264.html,/s/1o8qNB8Q Python 看完视频,建议你了解一下Python可以做什么: 1.网站后端程序员:使用它单间网站,后台服务比较容易维护。如:Gmail 、Youtube、知乎、豆瓣;

2.自动化运维:自动化处理大量的运维任务; 3.数据分析师:快速开发快速验证,分析数据得到结果; 4.游戏开发者:一般是作为游戏脚本内嵌在游戏中; 5.自动化测试:编写为简单的实现脚本运用在Selenium/lr,可实现自动化; 6.网站开发:借助django,flask框架自己搭建网站; 7.爬虫获取或处理大量信息:批量下载美剧、运行投资策略、爬合适房源、系统管理员的脚本任务等。 千锋教育Python培训课程共23周10大阶段力造Python全栈工程师,直击月薪15000!内容包括3.Python语言基础2.7.3、Python 3.x 进阶与python第三方库、PyQt开发、Linux指令、Linux Shell、Linux python运维自动化、KaliLinux 安全课程、Linux 网站后台-python开发、Python 数据库MySQL Oracle开发、HTML5基础CSS基础。 当然,没说完,还有Java-Script JQuery基础、Python项目爬虫,服务集群,网站后台,微信公众号开发、Python机器学习与数据挖掘,数据分析

Python期末复习题(必考)

2018-2019学年第一学期python练习专业、班级:__________ 学号:____________姓名:____________ Python语言基础 一、单选题 1.在Python中,合法的标识符是【】。 A. _ B. 3C C. it's D. str 2. Python表达式中,可以使用【】控制运算的优先顺序。 A.圆括号() B.方括号[] C.花括号{} D.尖括号<> 3.以下Python注释代码,不正确的是【】。 A. #Python注释代码 B. #Python注释代码1 #Python注释代码2 C. """Python文档注释""" D. //Python 注释代码 4.为了给整型变量x、y、z赋初值10,下面正确的Python赋值语句是【】。 A. xyz=10 B. x=10y=10z=10 C. x=y=z=10 D. x=10,y=10,z=10 5.为了给整型变量x、y、z赋初值5,下面正确的Python赋值语句是【】。 A. x=5;y=5;z=5 B. xyz=5 C. x,y,z= 5 D. x=5,y=5,z=5 6.已知x=2;y=3,复合赋值语旬x*=y+5执行后,x变量中的值是【】。 A. 11 B.16 C.13 D.26 7.整型变量x中存放了一个两位数,要将这个两位数的个位数字和十位数字交换位置,例如,13变成31,正确的Python表达式是【】。 A. (x%10)*10+x//10 B. (x%10)//10+x//10 C. (x/10)%10+x//10 D. (x%10)*10+x%10 二、填空题 1. Python中如果语句太长,可以使用作为续行符。 2. Python中在一行书写两条语句时,语句之间可以使用作为分隔符。 3. Python使用符号标示注释。 3. Python 表达式 4.5/2的值为。 4. Python表达式4.5//2的值为。 5. Python 表达式4.5%2的值为。 6. Python 表达式12/4-2+5*8/4%5/2的值为。 7. Python 语句a,b=3,4; a,b= b,a; print(a,b)的结果是。 8.已知x=5;y=6,复合赋值语旬x*=y+10执行后,x变量中的值是。 9. 写出下面程序的执行结果__________________。 x=345 a=x//100

PythonWeb开发基础教程教案PDF版进店另有word版或PPT版

教案 课程名称Python Web开发基础教程课程代码 系(部) 教研室(实验室) 授课教师 职称 年月

课程 名称 Python Web开发基础教程总计: 62 学时课程 类别 专业课课程代码 授课教师学分 讲授: 44 学时 实验: 22 学时 其他:学时 授课对象教材 和主要参考资料教材:夏帮贵《Python Web开发基础教程》人民邮电出版社 2019.12 参考资料:[美] Julia Elman(茱莉亚·埃尔曼),[美] Mark Lavin(马克·拉温)著,侯荣涛,吴磊译《轻量级Django》,中国电力出版社,2016.10 课程简介 Python 因其功能强大、简单易学、开发成本低廉,已成为深受广大应用程序开发人员喜爱的程序设计语言之一。Python 被广泛应用到各种领域,从简单的文字处理,到Web 应用和游戏开发,甚至于数据分析、人工智能和航天飞机控制,Python 都能胜任。随着网络的广泛普及,Web 应用开发已成为开发人员的必备技能之一。Python 具备上百种Web 开发框架,使用Web 框架在Python 中开发Web 应用,可以极大地提高开发效率。 Django 是Python 世界中成熟的Web 框架。Django 功能全面,各模块之间紧密结合。由于Django 提供了丰富、完善的文档,因此开发人员可以快速掌握Python Web 开发知识并及时解决学习中遇到的各种问题 《Python Web开发基础教程》课程作为Python Web开发入门教学课程,讲授内容主要包括:开发环境配置、Django 配置、URL 分发、模型和数据库、视图、模板、表单、Django 工具等内容。 本课程是一门实践性非常强的课程,因此在教学过程中应注重理论紧密联系实际和加强实习环节的教学,通过实验要掌握Python Web开发方法技巧。 教学难点重点第 1 章Python Web 开发起步第 2 章Django 配置 第 3 章URL 分发 第 4 章模型和数据库 第 5 章视图 第 6 章模板 第7 章表单 第8 章Django工具 第9 章Python在线题库

python基础重点梳理笔记

Python变量和数据类型 变量本身类型不固定的语言称之为动态语言,与之对应的是静态语言。静态语言在定义变量时必须指定变量类型。 如果字符串本身包含'怎么办?比如我们要表示字符串I'm OK ,这时,可以用" "括起来表示:"I'm OK" 类似的,如果字符串包含",我们就可以用' '括起来表示:'Learn "Python" in imooc'如果字符串既包含'又包含"怎么办? 这个时候,就需要对字符串的某些特殊字符进行转义,Python字符串用\进行转义:'Bob said \"I\'m OK\".' 字符串前面加个前缀r,表示这是一个raw 字符串,里面的字符就不需要转义了。例如:r'\(~_~)/ \(~_~)/' 表示多行字符串,可以用'''...'''表示: ''' Line 1 Line 2 Line 3''' 还可以在多行字符串前面添加 r ,把这个多行字符串也变成一个raw字符串: Python在后来添加了对Unicode的支持,以Unicode表示的字符串用u'...'表示, 比如:print u'中文' Python中布尔类型 and 和 or 运算的一条重要法则:短路计算。 1. 在计算 a and b 时,如果 a 是 False,则根据与运算法则,整个结果必定为 False,因此返回 a;如果 a 是 True,则整个计算结果必定取决与 b,因此返回 b。 2. 在计算 a or b 时,如果 a 是 True,则根据或运算法则,整个计算结果必定为 True,因此返回 a;如果 a 是 False,则整个计算结果必定取决于b,因此返回 b。

Python基础+练习

1.什么是python? python是一种解释型的、面向对象的、带有动态语义的高级程序设计语言。 2.python的优点 创始人评价:简单、优雅、明确 简单体现在如果你的母语是英语,写python脚本就像写文章,很简单; 优雅体现在python的格式,比如缩进来确定代码块,可避免编程人员进行复杂的嵌套; 明确体现在解决问题的方法只有一种最优选项,而perl语言是每个问题有很多最优解,但不利于团队协作; 有强大的第三方库模块,需要实现一复杂功能,只需要调用现有的库,可快速实现功能。20多年的发展,各种库都已经完备,比如:邮件库,爬虫库...... 可跨平台移植,java有Java的虚拟机,python同样; 是一种面向对象的语言; 是一种可扩展的语言(与C,C++,Java结合) 3.python的缺点 代码执行速度慢,相比C语言,不过现在python的异步并发框架导致执行速度慢; python是开源的编程语言,代码不能加密,当然有相应的工具可以将python代码转换为exe的二进制可执行码,但是反解码也很容易;

4.python的使用 1)交互式使用 2)脚本使用 测试: /usr/bin/env python ##会直接读取你当前的python版本,然后执行 /usr/bin/python ##仅仅只会读取编写的版本 coding:utf-8 ##转译中文

coding=utf-8 encoding:utf-8 encoding=utf-8 5.字符编码: ACCII:1字节=8位,2^8=256 Unicode:2字节=16位,2^16=65536 英文-> 2字节中文-> 2字节 utf-8:英文->1 字节,中文->3字节 GB2312:2字节 内存读取:Unicode 存在硬盘:utf-8 字符的编码(encode): unicode->utf-8 ##都是2字节,提升效率 字符的解码(decode): utf-8->unicode ##英文1字节,中文3字节,节省空间 >>> name_unicode = u"hello" >>> type(name_unicode) >>> name_utf8 = name_unicode.encode('utf-8') >>> type(name_utf8)

鱼c小甲鱼零基础学python全套课后题

第一节课 0. Python 是什么类型的语言? Python是脚本语言 脚本语言(Scripting language)是电脑编程语言,因此也能让开发者藉以编写出让电脑听命行事的程序。以简单的方式快速完成某些复杂的事情通常是创造脚本语言的重要原则,基于这项原则,使得脚本语言通常比C语言、C++语言或Java 之类的系统编程语言要简单容易。 也让脚本语言另有一些属于脚本语言的特性: ?语法和结构通常比较简单 ?学习和使用通常比较简单 ?通常以容易修改程序的“解释”作为运行方式,而不需要“编译” ?程序的开发产能优于运行性能 一个脚本可以使得本来要用键盘进行的相互式操作自动化。一个Shell脚本主要由原本需要在命令行输入的命令组成,或在一个文本编辑器中,用户可以使用脚本来把一些常用的操作组合成一组串行。主要用来书写这种脚本的语言叫做脚本语言。很多脚本语言实际上已经超过简单的用户命令串行的指令,还可以编写更复杂的程序。 1. IDLE 是什么? IDLE是一个Python Shell,shell的意思就是“外壳”,基本上来说,就是一个通过键入文本与程序交互的途径!像我们Windows那个cmd窗口,像Linux那个黑乎乎的命令窗口,他们都是shell,利用他们,我们就可以给操作系统下达命令。同样的,我们可以利用IDLE这个shell与Python进行互动。 2. print() 的作用是什么? print() 会在输出窗口中显示一些文本(在这一讲中,输出窗口就是IDLE shell 窗口)。 3. Python 中表示乘法的符号是什么? Python中的乘号是*(星号)。 4. 为什么>>>print('I love https://www.sodocs.net/doc/1516097264.html, ' * 5) 可以正常执行, 但>>>print('I love https://www.sodocs.net/doc/1516097264.html, ' + 5) 却报错?

python基础100练习题

实例001:数字组合 题目有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?程序分析遍历全部可能,把有重复的剃掉。 total=0 for i in range(1,5): for j in range(1,5): for k in range(1,5): if ((i!=j)and(j!=k)and(k!=i)): print(i,j,k) total+=1 print(total) 12345678 简便方法用itertools中的permutations即可。 import itertools sum2=0 a=[1,2,3,4] for i in itertools.permutations(a,3): print(i) sum2+=1 print(sum2) 12345678 实例002:“个税计算” 题目企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?程序分析分区间计算即可。 profit=int(input('Show me the money: ')) bonus=0 thresholds=[100000,100000,200000,200000,400000] rates=[0.1,0.075,0.05,0.03,0.015,0.01] for i in range(len(thresholds)): if profit<=thresholds[i]: bonus+=profit*rates[i] profit=0 break else: bonus+=thresholds[i]*rates[i] profit-=thresholds[i] bonus+=profit*rates[-1]

Python基础入门教程

Python基础入门教程 你可能已经听说过很多种流行编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的JavaScript语言,那么你知道Python是一种怎样的计算机程序设计语言吗?下面应广大读者需求,给大家呈上一篇Python基础入门教程的文章。 Python 是由Guido van Rossum 在八十年代末和九十年代初,在荷兰国家数学和计算机科学研究所设计出来的。 Python 本身也是由诸多其他语言发展而来的,这包括ABC、Modula-3、C、C++、Algol-68、SmallTalk、Unix shell 和其他的脚本语言等等。 像Perl 语言一样,Python 源代码同样遵循GPL(GNU General Public License)协议。 现在Python 是由一个核心开发团队在维护,Guido van Rossum 仍然占据着至关重要的作用,指导其进展。 Python 特点

? 1.易于学习:Python有相对较少的关键字,结构简单,和一个明确定义的语法,学习起来更加简单。 ? 2.易于阅读:Python代码定义的更清晰。 ? 3.易于维护:Python的成功在于它的源代码是相当容易维护的。 ? 4.一个广泛的标准库:Python的最大的优势之一是丰富的库,跨平台的,在UNIX,Windows和Macintosh兼容很好。 ? 5.互动模式:互动模式的支持,您可以从终端输入执行代码并获得结果的语言,互动的测试和调试代码片断。 ? 6.可移植:基于其开放源代码的特性,Python已经被移植(也就是使其工作)到许多平台。 ?7.可扩展:如果你需要一段运行很快的关键代码,或者是想要编写一些不愿开放的算法,你可以使用C或C++完成那部分程序,然后从你的Python程序中调用。 ?8.数据库:Python提供所有主要的商业数据库的接口。 ?9.GUI编程:Python支持GUI可以创建和移植到许多系统调用。 ?10.可嵌入: 你可以将Python嵌入到C/C++程序,让你的程序的用户获得"脚本化"的能力。

相关主题