全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
2040 17
2021-12-10
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.


二维码

扫码加我 拉你入群

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

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

全部回复
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,]
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群