In my last post, I showed you how to install Python and set up Stata to use Python. Now, we’re ready to use Python. There are three ways to use Python within Stata: calling Python interactively, including Python code in do-files and ado-files, and executing Python script files. Each is useful in different circumstances, so I will demonstrate all three. The examples are intentionally simple and somewhat silly. I’ll show you some more complex examples in future posts, but I want to keep things simple in this post.
Calling Python interactively
You can use Python interactively within Stata by typing python in Stata’s Command window.
Stata reminds you that you can type end to exit Python when you are finished. You can now type Python code in the Command window. For example, you could instruct Python to print a phrase.
You can also use Python as an interactive calculator.
Then, you can type end to exit Python and return to Stata.
You can also simply type python: followed by a Python statement. Python will run the statement and then return to Stata. Here is a simple example:
You can also use this syntax for quick calculations:
Python in do-files and ado-files
You can use python: followed by a simple command such as
in do-files and ado files, just as you can interactively.
You can also use Python code blocks to run multiple Python statements within do-files and ado-files. A Python code block begins with python and ends with end, as we saw in the interactive example above. Example 1 below is a do-file that begins with a Stata display command, begins a Python code block with python, executes two Python print() statements, ends the Python code block with end, and finishes with a Stata display command.
Example 1: hello.do
When we type do hello, it is easy to see the Python code block in the output because the code block begins and ends with rows of dashed lines.
There are two important things to know about code blocks. First, you can begin a code block with python or with python:. Both will run the code block, but they behave differently if Python encounters an error within the code block. If you begin the code block with python and Python encounters an error, Python will continue to execute the remaining code in the code block. If you begin the code block with python: and Python encounters an error, Python will return control to Stata without executing the remaining Python code. A stack trace will be printed, and an error code will be issued in both cases.
Second, indentation within Python code blocks is functional rather than aesthetic. For example, you may be tempted to write a Python code block and indent the code within the block as in Example 2.
Example 2: hello2.do
But Python uses indentation to “determine the grouping of statements” (Python Documentation 2.1.7). Python returns an indentation error when we run Example 2.
You can read more about the use of indentation in the Python documentation.
Running Python scripts
You can also run Python scripts within Stata. A Python script is simply a collection of Python statements saved in a file with a .py extension. Example 3 shows the contents of a Python script named hello.py.
Example 3: hello.py
We can run the Python script by typing python script followed by the name of the script we wish to run.
This method of running Python within Stata is useful when you encounter a Python script that is written by someone else. It is also useful for saving a collection of Python functions. I’ll show you how to pass arguments from Stata to Python scripts in a future blog post.
You can use python script in the Command window, do-files, or ado-files.
Conclusion
In this blog post, I have shown you three different ways to run Python within Stata. Each of them is useful in different circumstances, and I will demonstrate their uses in future blog posts. Next time, I will show you how to download and install Python packages.
近期,武汉大学经济与管理学院联合Stata软件中国授权经销商北京天演融智软件有限公司(科学软件网)在七-八月份举办Stata系列专题讲座以及Stata软件研讨会,得到了大家的一致**。在此,科学软件网为大家提供三场讲座的回放以及“Stata洞察数据科学研讨会” 中李春涛老师主讲的主题为 “中文地图与Stata” 和 肖光恩老师主讲的主题为 “新冠疫情数据的可视化与建模方法” 的演讲回放视频,全部免费,可直接扫码(页面下方直接点 试听 )免费观看。
北京天演融智软件有限公司(科学软件网)是Stata软件在中国的授权经销商,为中国的Stata用户提供优质的软件销售和培训服务。
词条
词条说明
StataCorp LLC将于11月3日举办2022年Stata经济虚拟研讨会--Stata进行经济理论和应用研究的盛会。本次会议邀请了**Stata经济学用户进行演讲,虚拟会议让您无论身在何处都可以体验这一为期一天的活动。座位有限,报满截止!登录:科学软件网—培训—收费培训 了解详情。 演讲者会议议程时间:11月3日(注意:以上时间为CDT时间)方式:在线费用:$4
近日,新型冠状病毒感染的肺炎疫情牵动着全国人民的心,科学软件网响应教育部“停课不停学”的号召,“疫”不容辞,决定对旗下全线软件视频课程进行较大限度优惠,让大家足不出户也能随时随地充电。即日起至2月28日,在科学软件网(**此平台)购买所有视频课程,都可享有8.5折,活动结束后恢复原价。参加优惠活动课程列表以上课程均有免费试听视频,欢迎试看。如果您有购买计划,详情请登陆科学软件网-培训-视频课程。
CaRIne Crystallography 晶体学软件介绍
CaRIne Crystallography 材料科学软件,新版4.0有一个架构和生动的用户界面,且在仿真模拟和模块分析过程中,可以进行简单和复杂的(多任务)集成。例如立体投影、X射线粉末衍射、单晶体以及3D和2D中的 reciprocical格,这些不仅是电子衍射的**模块,而且对写报告也非常有帮助。 当这些模块不可用时,那么3.1版本可以与4.0版本的功能交付使用
活动 | Mathematica + System Modeler bundle 20% off
疫情期间,Wolfram推出了系列公益培训并且免费分享了大量的COVID-19技术资源。近期,Wolfram又推出了为期较长,力度较大的促销活动。即日起,购买Mathematica、System Modeler 享有8折优惠。中小学购买Wolfram Alpha Notebook Edition,享受5折优惠。活动有效期至2020年12月31日。Mathematica 在其三十年的开发历程中,在技
公司名: 北京天演融智软件有限公司
联系人: 张经理
电 话:
手 机: 18610814366
微 信: 18610814366
地 址: 北京海淀海淀上地东路35号院1号楼3层1-312318、1-312-319
邮 编: