Python basic learning code execution environment
class C(object): def __call__(self, *args, **kwargs): print "I'm callable! called with args:\n",args c = C() c('a',1) single_code = compile("print 'hello,world!'",'','single') exec(single_code) eval_code = compile('100*3','','eval') print eval(eval_code) #exec_code = compile("""req = input('input:') #for eachnum in range(req): # print eachnum""",'','exec') #exec(exec_code) exec """x = 0 print 'x is currently:',x while x < 5: x+=1 print 'incrementing x to:',x """ #f = open('c14.py') #exec f #print f.tell() #print f.close() #from os.path import getsize #getsize('c14.py') #f.seek(0) #exec f #loopmake dashes = '\n' + '-' * 50 exec_dict = { 'f':""" for %s in %s: print %s """, 's':""" %s = 0 %s = %s while %s < len(%s): print %s[%s] %s = %s + 1 """, 'n':""" %s = %d while %s < %d: print %s %s = %s + %d """ } def main(): ltype = raw_input('Loop type?[for/while]') dtype = raw_input('Data type?[number/seq]') if dtype == 'n': start = input('start value?:') stop = input('ending value?:') step = input('steping value?:') seq = str(range(start,stop,step)) def foo(): return True def bar(): 'bar() does not much' return True foo.__doc__ = 'foo() does not much' foo.tester = """ if foo(): print 'passed' else: print 'failed' """ for eachattr in dir(): obj = eval(eachattr) if isinstance(obj,type(foo)): if hasattr(obj,'__doc__'): print '\nfunction "%s" has a doc string:\n\t%s' % (eachattr,obj.__doc__) if hasattr(obj,'tester'): print '\nfunction "%s" has tester' % eachattr exec(obj.tester) else: print '%s function has no tester' % eachattr else: print '%s is not a function' % eachattr import os #print os.system('ping www.qq.com') f = os.popen('dir') data = f.readlines() f.close() print data ## 替换os.system from subprocess import call res = call(('dir'),shell=True) ## 替换os.popen from subprocess import PIPE,Popen f = Popen(('wmic','diskdrive'),stdout=PIPE).stdout data = f.readlines() f.close() print data import sys def usage(): print 'At least 2 arguments' print 'usage: args.py arg1 arg2 [arg3....]' sys.exit(1) argc = len(sys.argv) #if argc < 3: # usage() prev_exit_func = getattr(sys,'exitfunc',None) def my_exit_func(old_exit=prev_exit_func): if old_exit is not None and callable(old_exit): old_exit() sys.exitfunc = my_exit_func def my_exit(): print 'exit python' sys.exitfunc = my_exit print 'hello,begin exit.' sys.exit(1)
The above is the content of the execution environment of Python basic learning code. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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 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.

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

The article discusses the role of virtual environments in Python, focusing on managing project dependencies and avoiding conflicts. It details their creation, activation, and benefits in improving project management and reducing dependency issues.
