楼主: 数据洞见
1800 17

Getting Started with Python [推广有奖]

  • 0关注
  • 0粉丝

硕士生

43%

还不是VIP/贵宾

-

威望
0
论坛币
231430 个
通用积分
447.7601
学术水平
5744 点
热心指数
5735 点
信用等级
5732 点
经验
263427 点
帖子
147
精华
0
在线时间
172 小时
注册时间
2021-6-20
最后登录
2024-9-7

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Getting Started with Python
With Anaconda installed and Jupyter notebooks up and running, you have everything in place to get started with Python. Although this chapter doesn’t go much further than the basics, it still covers a lot of ground. If you are at the beginning of your coding career, there may be a lot to digest. However, most concepts will get clearer once you use them in later chapters as part of a practical example, so there’s no need to worry if you don’t understand something fully the first time around. Whenever Python and VBA differ significantly, I will point this out to make sure you can transition from VBA to Python smoothly and are aware of the obvious traps. If you haven’t done any VBA before, feel free to ignore these parts.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Started getting python Start With

沙发
数据洞见 发表于 2021-12-10 09:06:19 |只看作者 |坛友微信交流群
Data Types
Python, like every other programming language, treats numbers, text, booleans, etc. differently by assigning them a different data type. The data types that we will use most often are integers, floats, booleans, and strings. In this section, I am going to introduce them one after another with a few examples. To be able to understand data types, though, I first need to explain what an object is.
Objects
InPython, everythingis an object, including numbers, strings, functions, and everything else that we’ll meet in this chapter. Objects can make complex things easy and intuitive by giving you access to a set of variables and functions. So before anything else, let me say a few words about variables and functions!
Variables
InPython, a variableis a name that you assign to an object by using the equal sign. In the first line of the following example, the name ais assigned to the object 3:
In[1]:a=3b=4a+bOut[1]: 7
This works the same for all objects, which is simpler comparedto VBA, where you use the equal sign for data types like numbers and strings and the Setstatement for objects like workbooks or worksheets. In Python, you change a variable’s type simply by assigning it to a new object. This isreferred to as dynamic typing:
In[2]:a=3print(a)a="three"print(a)3
three
Unlike VBA, Python is case-sensitive, so aand Aare twodifferent variables. Variable names must follow certain rules:They must start with either a letter or an underscore
They must consist of letters, numbers, and underscores
After this short introduction to variables, let’s see how we can make function calls!

使用道具

藤椅
数据洞见 发表于 2021-12-10 09:06:31 |只看作者 |坛友微信交流群
Functions
I will introduce functions with a lot more detail later in this chapter. For now, you should simply know how to call built-in functions like printthat we used in the previous code sample. To call a function, you add parentheses to the function name and provide the arguments within the parentheses, which is pretty much equivalent to the mathematical notation:
function_name(argument1,argument2,)
Let’s now look at how variables and functions work in the context of objects!

使用道具

板凳
数据洞见 发表于 2021-12-10 09:07:08 |只看作者 |坛友微信交流群
Attributes and methods
In thecontext of objects, variables are called attributesand functions are called methods: attributes give you access to the data of an object, and methods allow you to perform an action. To access attributes and methods, you use thedot notation like this: myobject.attributeand myobject.method()
Let’s make this a bit more tangible: if you write a car racing game, you would most likely use an object that represents a car. The carobject could have a speedattribute that allows you to get the current speed via car.speed, and you might be able to accelerate the car by calling the accelerate method car.accelerate(10), which would increase the speed by ten miles per hour.
The type of an object and with that its behavior is defined by a class, so the previous example would require you to write a Carclass. The process of getting a carobject out of a Carclass is called instantiation, and you instantiate an object by calling
the classin the same way as you call a function: car = Car(). We won’t write our own classes in this book, but if you are interested in how this works, have a look at
Appendix  C
We will use a first object method in the next section to make a text string uppercase, and we will get back to the topic of objects and classes when we talk about datetimeobjects toward the end of this chapter. Now, however, let’s move on with those objects that have a numeric data type!

使用道具

报纸
数据洞见 发表于 2021-12-10 09:07:36 |只看作者 |坛友微信交流群
Data Structures
Pythonoffers powerful data structures that make working with a collection of objects really easy. In this section, I am going to introduce lists, dictionaries, tuples, and sets. While each of these data structures has slightly different characteristics, they are all able to hold multiple objects. In VBA, you may have used collections or arrays to hold multiple values. VBA even offers a data structure called dictionary that works conceptually the same as Python’s dictionary. It is, however, only available on the Windows version of Excel out of the box. Let’s get started with lists, the data structure that you will probably use most.

使用道具

地板
数据洞见 发表于 2021-12-10 09:08:03 |只看作者 |坛友微信交流群
Lists
Listsare capable of holding multiple objects of different data types. They are so versatile that you will use them all the time. You create a list as follows:
[element1,element2,]

使用道具

7
数据洞见 发表于 2021-12-10 09:08:25 |只看作者 |坛友微信交流群
Sets
Setsarecollections that have no duplicate elements. While you can use them for set theory operations, in practice they often help you to get the unique values of a list or a tuple. You create sets by using curly braces:
{element1,element2,}
To get the unique objects in a list or a tuple, usethe setconstructor like so:
In[79]:set(["USD","USD","SGD","EUR","USD","EUR"])Out[79]: {'EUR', 'SGD', 'USD'}

使用道具

8
数据洞见 发表于 2021-12-10 09:08:43 |只看作者 |坛友微信交流群
Code Blocks and the pass Statement
A code blockdefinesa section in your source code that is used for something special. For example, you use a code block to define the lines over which your program is looping or it makes up the definition of a function. In Python, you define code blocks by indenting them, not by using keywords like in VBA or curly braces like in most other languages. This is referred toas significant white space.

使用道具

9
数据洞见 发表于 2021-12-10 09:09:22 |只看作者 |坛友微信交流群
The for and while Loops
Ifyou need to do something repeatedly like printing the value of ten different variables, you are doing yourself a big favor by not copy/pasting the print statement ten times. Instead, use a forloop to do the work for you. forloops iterate over the items of a sequence like a list, a tuple, or a string (remember, strings are sequences of characters). As an introductory example, let’s create a forloop that takes each element of the currencieslist, assigns it to the variable currencyand prints it—one after another until there are no moreelements in the list:
In[89]:currencies=["USD","HKD","AUD"]forcurrencyincurrencies:print(currency)USD
HKD
AUD
As a side note, VBA’s For Eachstatement is close to how Python’s forloop works. The previous example could be written like this in VBA:
DimcurrenciesAsVariantDimcurrAsVariant'currency is a reserved word in VBAcurrencies=Array("USD","HKD","AUD")ForEachcurrIncurrenciesDebugPrintcurrNext
In Python, if you need a counter variable in a forloop, the rangeor enumeratebuilt-ins can help you with that. Let’s first look at range, which provides a sequence of numbers: you call it by either providing a single stopargument or by providing a startand stopargument, with an optional stepargument. Like with slicing, startis inclusive, stopis exclusive, and stepdetermines the step size, with 1being the default:
range(stop)range(start,stop,step)
rangeevaluates lazily, which means that without explicitly asking for it, you won’t see the sequence it generates:
In[90]:range(5)Out[90]: range(0, 5)
Converting the range to a list solves this issue:
In[91]:list(range(5))# stop argumentOut[91]: [0, 1, 2, 3, 4]In[92]:list(range(2,5,2))# start, stop, step argumentsOut[92]: [2, 4]
Most of the time, there’s no need to wrap rangewith a list, though:
In[93]:foriinrange(3):print(i)
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
yunnandlg + 100 + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 100  论坛币 + 100  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

使用道具

10
数据洞见 发表于 2021-12-10 09:09:42 |只看作者 |坛友微信交流群
List, Dictionary, and Set Comprehensions
List, dictionary, and set comprehensions are technically a way to create the respective data structure, but they often replace a forloop, which is why I am introducing them here. Assume that in the following list of USD currency pairs, you’d like to pick out those currencies where USD is quoted as the second currency. You could write the following forloop:
In[101]:currency_pairs=["USDJPY","USDGBP","USDCHF","USDCAD","AUDUSD","NZDUSD"]

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-11-5 14:42