问一个python问题

2024-05-12 15:26

1. 问一个python问题

try:    x=input('Enter the first number:')    y=input('Enter the second number:')    print x/yexcept ZeroDivisionError:    print "The second number cannot be zero"except TypeError:    print "That wasnot a number, was it?" except NameError:    print "That was not a variab name"input的输入直接被当成python表达式评估,输入d,就表示变量 d,输入"d"表示字符串,才会触发TypeError异常

问一个python问题

2. 问一个python问题

*p会把一个迭代器中的元素一个一个压栈,字典不行。
**p会把键值对压栈,func(**p) => func(arg1=1,arg2=2)形式
 
If there are more positional arguments than there are formal parameter slots, 
a TypeError exception is raised, unless a formal 
parameter using the syntax *identifier is present; in this case, that formal 
parameter receives a tuple containing the excess positional arguments (or an 
empty tuple if there were no excess positional arguments).

If any keyword argument does not correspond to a formal parameter name, a TypeError exception is raised, unless a formal 
parameter using the syntax **identifier is present; in this case, that formal 
parameter receives a dictionary containing the excess keyword arguments (using 
the keywords as keys and the argument values as corresponding values), or a 
(new) empty dictionary if there were no excess keyword arguments.

If the syntax *expression appears in the function call, expression must evaluate to 
an iterable. Elements from this iterable are treated as if they were additional 
positional arguments; if there are positional arguments x1, ..., 
xN, and expression evaluates to a sequence y1, ..., 
yM, this is equivalent to a call with M+N positional arguments 
x1, ..., xN, y1, ..., yM.

A consequence of this is that although the *expression syntax may appear after some keyword 
arguments, it is processed before the keyword arguments (and the **expression argument, if 
any – see below). So:


>>> def f(a, b):
...  print(a, b)
...
>>> f(b=1, *(2,))
2 1
>>> f(a=1, *(2,))
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: f() got multiple values for keyword argument 'a'
>>> f(1, *(2,))
1 2
It is unusual for both keyword arguments and the *expression syntax to be 
used in the same call, so in practice this confusion does not arise.

If the syntax **expression appears in the function call, expression must evaluate to 
a mapping, the contents of which are treated as additional keyword arguments. In 
the case of a keyword appearing in both expression and as an explicit keyword argument, a TypeError exception is raised.

Formal parameters using the syntax *identifier or **identifier cannot be used as positional argument slots 
or as keyword argument names.

A call always returns some value, possibly None, unless it raises an exception. How this value is 
computed depends on the type of the callable object.

3. 问一个python问题

你学过java吧
def __init__(self)相当于java里类的构造函数
def eat(self)相当于java里类的方法
self就是对象本身(有点类似于java的this),可以用其他变量名表示,但是python习惯上约定为self,一个对象实例调用自己的方法的时候,python会自动将本身作为第一个参数,比如这里
b = bird()b.eat()#这里会调用b的eat方法,但是第一个参数即self会由python自动设置

问一个python问题

4. 问一个python小问题

在未知参数个数时使用。
在参数名之前使用一个星号,就是让函数接受任意多的位置参数。
如:
def story(*argv)
...

story(1,2,3,)

python在参数名之前使用2个星号来支持任意多的关键字参数。
story(foo='bar', spam='eggs')

5. 问一个python小问题

$ python3Python 3.2.3 (default, Feb 20 2013, 17:02:41) [GCC 4.7.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> table = "".maketrans({"1": "{one}", "2": "{two}"})>>> print("12".translate(table)){one}{two}>>>

问一个python小问题

6. 有没有会用Python编写一个简单的建模股票价格的小程序?能够对股票数据进行简单预测即可!求助!

虽然懂python 但是不懂股票,
采用random()可以么,哈哈

7. 股票方面,那种量化分析软件比较靠谱?求推荐。

我给你说下,

股票方面,那种量化分析软件比较靠谱?求推荐。

8. 用Python 进行股票分析 有什么好的入门书籍或者课程吗

问题不对,你拿股票当工科看了,理工学院里可没有一个股票分析专业。股票或者投资这行有两个特点,1. 除了市场数据必看,没有什么理论必看。理论跟你实际操作相比是垃圾,这么说不过分;2. 实际能赚钱的经验,没有人会公开的。公开会导致失效,会引来对手盘,没人会跟自己过不去。能赚钱的人基本也没什么兴趣出书或教课。所以,别嫌给你浇冷水, 如果你想要书籍或者课程的话,就在理工类里面挑一个接近投资的专业吧,比如 quants。自己没方向的话,恐怕想求助也难。我是做这个的,但完全是自己摸索。Python 是自学,股票分析也是自己攒经验值。我的博客或许能给你点启发: Jacky Liu's Blog , 但最多是启发而已。你得想出你自己的点子,然后自己去跟市场求证,谢谢 ~
最新文章
热门文章
推荐阅读