def func1(): dict1 = {} dict2 = {'name':'earth','port':80} return dict1,dict2 def func2(): return dict((['x',1],['y',2])) def func3(): adict = {}.fromkeys(['x','y'],23) return adict def func4(): alist = {'name':'earth','port':80} for keys in alist.keys(): print "%s %s" % (keys,alist[keys]) def func5(): alist = {'name':'earth','port':80} for keys in alist: print "%s %s" % (keys,alist[keys]) def func6(akey): alist = {'name':'earth','port':80} if akey in alist: return True else: return False def func7(akey): alist = {'name':'earth','port':80} if alist.has_key(akey): return True else: return False def func8(): alist = {'name':'earth','port':80} print 'host %(name)s is running on %(port)d' % alist def func9(akey): alist = {'name':'earth','port':80} if akey in alist: del alist[akey] return True else: return False def func10(): alist = {'name':'earth','port':80} alist.clear() return alist def func11(): alist = {'name':'earth','port':80} del alist def func12(akey): alist = {'name':'earth','port':80} if akey in alist: return alist.pop(akey) def func13(): alist = {'name':'earth','a':80} blist = {'name':'earth','z':8080} return cmp(alist,blist) def func14(): alist = zip(('x','y'),(1,2)) blist = dict([('xy'[i-1],i) for i in range(1,3)]) return dict(alist),blist def func15(): adict = {'name':'earth','port':80} bdict = adict.copy() return bdict,len(bdict) def func16(): adict = {'name':'earth','port':80} bdict = {'name':'earth','port':8080} print adict.keys() print adict.values() print adict.items() adict.update(bdict) print adict def func17(): adict = {'name':'earth','port':80} for keys in sorted(adict): print 'adict %s has value %s' % (keys,adict[keys]) def func18(akey): adict = {'name':'earth','port':80} if adict.get(akey): return True else: return 'no such key!' def func19(): adict = {'name':'earth','port':80} bdict = {}.fromkeys('abc') print bdict return adict.setdefault('name','wycqhost') import time nowtime = time.time() def gettime(nowtime): return time.strftime('%Y/%m/%d %H:%M:%S',time.localtime(nowtime)) login = {} def newuser(): prompt = 'login desired:' name = '' while True: name = raw_input(prompt) if login.has_key(name): prompt = 'name taken,try another:' continue else: break pwd = raw_input('passwd:') login[name] = [abs(hash(pwd))] login[name].append(0) print login def olduser(): nowtime = time.time() name = raw_input('login:') if name not in login: choose = raw_input('will you create a new user(y/n)') if choose.lower()[0] == 'y': newuser() else: pass showmenu() else: pwd = raw_input('passwd:') passwd = login.get(name)[0] if abs(hash(passwd)) == abs(hash(pwd)): if login[name][1] == 0: print login print 'welcome back',name,'you first time loggin' else: print 'welcome back',name if nowtime - login[name][1] <= 14400: print 'you are already logged at time',gettime(login[name][1]) login[name][1] = nowtime else: print 'login incorrect' return def showuser(): print 'show all user:' for user in login.keys(): print user def deleteuser(): duser = raw_input('delete user:').lower() if duser in login.keys(): del login[duser] else: print 'user %s is not exists' % duser showmenu() def showmenu(): prompt = ''' (n)ew user login (o)ld user login (s)how all user (d)elete user (q)uit enter choice: ''' done = False while not done: chosen = False while not chosen: try: choice = raw_input(prompt).strip()[0].lower() except (EOFError,KeyboardInterrupt): choice = 'q' print 'you picked %s' % choice if choice not in 'noqds': print 'invalid option,try again' else: chosen = True if choice == 'q': done = True if choice == 'n': newuser() if choice == 'o': olduser() if choice == 'd': deleteuser() if choice == 's': showuser() #if __name__ == '__main__': # showmenu() def func20(): str1 = '093keffeoelgn' t = set(str1) s = frozenset(str1) return t == s def func21(): aset = set('xiewenbin') if 'x' in aset: print "x in aset" def func22(): aset = set('strings') aset.add('http') aset.update('httpx') aset.remove('http') aset -= set('x') for i in aset: print i del aset def func23(): aset = set('abc') bset = set('abcedf') return aset <= bset def func24(): aset = set('markshop') bset = frozenset('earthshop') print aset | bset print bset & aset print aset ^ bset print aset - bset def func25(): s = set('cheeseshop') u = frozenset(s) s |= set('xie') print s s = set(u) s &= set('shop') print s s = set(u) s -= set('shop') print s s = set(u) t = frozenset('bookshop') s ^= t print s print len(s) import os def func26(): frozenset(['a','b','c']) f = open('test.txt','w') for i in range(5): f.write('%d\n'%i) f.close() f = open('test.txt','r') print set(f) f.close() os.remove('test.txt') def func27(): alist = ['a','b'] blist = [1,2] print dict(zip(alist,blist)) def func28(): adict = {'a':1,'b':2,'c':3} bdict = {} for keys in adict.keys(): bdict[adict[keys]] = keys return bdict def func29(sstr,dstr,string,casemap=True): assert len(sstr) >= len(dstr) table = dict(zip(sstr,dstr)) print table if len(sstr) > len(dstr): temp = {}.fromkeys(sstr[len(dstr)]) table.update(temp) print table ls = [] for ch in string: if not casemap: if ch.lower() in table: ls.append(table[ch.lower()]) elif ch.upper() in table: ls.append(table[ch.upper()]) else: ls.append(ch) continue if ch in table: ls.append(table[ch]) else: ls.append(ch) ls = [ch for ch in ls if ch] print ls return " ".join(ls) def func30(sstr): alist = [chr((num + 13) % 26 + ord('a')) for num in range(26)] blist = [chr(num + ord('a')) for num in range(26)] table = dict(zip(blist,alist)) astr = "".join(alist).upper() bstr = "".join(blist).upper() table.update(dict(zip(bstr,astr))) ls = [] for ch in sstr: if ch in table: ls.append(table[ch]) else: ls.append(ch) return " ".join(ls) import random def func31(): alist = [random.randint(i,10) for i in range(10)] blist = [random.randint(i,10) for i in range(10)] aset = set(alist) bset = set(blist) print aset | bset print aset & bset
The above is the content of the image collection of Python basic learning code. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Python's statistics module provides powerful data statistical analysis capabilities to help us quickly understand the overall characteristics of data, such as biostatistics and business analysis. Instead of looking at data points one by one, just look at statistics such as mean or variance to discover trends and features in the original data that may be ignored, and compare large datasets more easily and effectively. This tutorial will explain how to calculate the mean and measure the degree of dispersion of the dataset. Unless otherwise stated, all functions in this module support the calculation of the mean() function instead of simply summing the average. Floating point numbers can also be used. import random import statistics from fracti

Serialization and deserialization of Python objects are key aspects of any non-trivial program. If you save something to a Python file, you do object serialization and deserialization if you read the configuration file, or if you respond to an HTTP request. In a sense, serialization and deserialization are the most boring things in the world. Who cares about all these formats and protocols? You want to persist or stream some Python objects and retrieve them in full at a later time. This is a great way to see the world on a conceptual level. However, on a practical level, the serialization scheme, format or protocol you choose may determine the speed, security, freedom of maintenance status, and other aspects of the program

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

This tutorial builds upon the previous introduction to Beautiful Soup, focusing on DOM manipulation beyond simple tree navigation. We'll explore efficient search methods and techniques for modifying HTML structure. One common DOM search method is ex

This article guides Python developers on building command-line interfaces (CLIs). It details using libraries like typer, click, and argparse, emphasizing input/output handling, and promoting user-friendly design patterns for improved CLI usability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
