python初学者编程指南
Python is a popular programming language created by Guido van Rossum who released it in 1991. Due to its rise in popularity, I thought it would be a good idea to write an article about the fundamentals of the language.
Python是由Guido van Rossum于1991年发布的一种流行的编程语言。由于它的流行,我认为写一篇有关该语言基础的文章是一个好主意。
If you’re starting out with Python and wondering why you might choose to learn this language, keep in mind that it is cross-platform, has a simple syntax, and is similar to English. In addition to this, Python is a high-level programming language, so a lot of work is done with a small amount of code.
如果您刚开始使用Python,并且想知道为什么选择学习这种语言,请记住它是跨平台的,具有简单的语法,并且类似于英语。 除此之外,Python是一种高级编程语言,因此只需少量代码即可完成大量工作。
Sold on Python and excited to progress? Great, let’s dive in.
在Python上出售并且对进步感到兴奋吗? 太好了,让我们潜入。
入门 (Getting Started)
Create a file called app.py and add the following simple code:
创建一个名为app.py的文件,并添加以下简单代码:
print("Hello Medium")
This is the most simple program — as you can see there is no semicolon.
这是最简单的程序-如您所见,没有分号。
Python Syntax
Python语法
Python uses almost no brackets and absolutely no semicolons, but it does use indention. When using a code editor like visual studio code, the indention will be added automatically.
Python几乎不使用方括号,并且绝对不使用分号,但是它确实使用缩进。 当使用类似Visual Studio代码的代码编辑器时,缩进将自动添加。
if 1 + 1 == 2:
print("1 + 1 = 2")
Comments
注释
Commenting in Python is done using a hashtag.
Python中的注释是使用#标签完成的。
if 1 + 1 == 2:
print("1 + 1 = 2")# This is a comment"""This is the way how to use multi-linecomments
"""
变数 (Variables)
As in every other programming language, Python has variables. Variables are like containers for numbers, strings, and booleans. Variables are very easy to create as follows:
与其他所有编程语言一样,Python也具有变量。 变量就像数字,字符串和布尔值的容器。 变量很容易创建,如下所示:
x = 1
y = 3
z = x * y
A variable can have a short name and has to start with a letter or an underscore, and they are case sensitive.
变量可以短名,并且必须以字母或下划线开头,并且区分大小写。
资料类型 (Data Types)
In programming, the data type is an important concept. Here are some examples:
在编程中,数据类型是一个重要的概念。 这里有些例子:
- String — A string is a sequence of characters String —字符串是字符序列
- Integer — An integer data type is a non-decimal number 整数—整数数据类型是非十进制数字
- Float — A float is a number with a decimal point or a number 浮点数—浮点数是带小数点的数字或数字
- Boolean — A Boolean represents TRUE or FALSE. 布尔值—布尔值表示TRUE或FALSE。
- Array — An array stores multiple values in one single variable. 数组—数组将多个值存储在一个变量中。
Getting the data type
获取数据类型
x = 5
print(type(x))# Outputs: <class 'int'>
基本运算符 (Basic Operators)
- + (Addition) +(加法)
- - (Subtraction) -(减法)
- * (Multiplication) *(乘法)
- / (Division) /(部门)
- % (Modules) %(模组)
- ** (Exponentiation) **(取幂)
馆藏 (Collections)
There are four collection data types in the Python programming language:
Python编程语言中有四种收集数据类型:
Lists
清单
A list is a collection that is ordered and changeable. It allows duplicate members. In Python, lists are written with square brackets.
列表是有序且可更改的集合。 它允许重复的成员。 在Python中,列表用方括号括起来。
mylist = ["one", "two", "three"]
print(mylist)
Tuple
元组
A tuple is a collection that is ordered and unchangeable. It allows duplicate members. In Python, tuples are written with round brackets.
元组是一个集合,是有序的 , 不可改变的 。 它允许重复的成员。 在Python中,元组用圆括号括起来。
mytuple = ("one", "two", "three")
print(mytuple)
Sets
套装
A set is a collection that is unordered and unindexed. It does not allow duplicate members.
集合是无序且无索引的集合。 它不允许重复的成员。
myset = {"one", "two", "three"}
print(myset)
Dictionaries
辞典
A dictionary is a collection that is unordered, changeable, and indexed. It does not allow duplicate members.
字典是无序,可变和索引的集合。 它不允许重复的成员。
myDict= {
"name": "Medium",
"url": "https://medium.com",
"date": "20-08-2020"
}
print(myDict)
数组 (Arrays)
Arrays are used to store multiple values in one single variable.
数组用于将多个值存储在一个变量中。
cars = ["Ford", "Audi", "Tesla"]
Accessing Elements
访问元素
You refer to an array element by referring to the index number.
您通过引用索引号来引用数组元素。
x = cars[0]
cars[0] = "Toyota"
The Length of an Array
数组的长度
x = len(cars)
条件语句 (Conditional Statements)
As mentioned with the indentions, conditional statements are created without brackets.
如缩进所述,条件语句创建时没有括号。
if 1 + 1 == 2
print("1 + 1 = 2")
elif 1 + 1 != 2
print("Error")
循环 (Loops)
As in other programming languages, Python has loops to perform repeating acts.
与其他编程语言一样,Python具有执行重复动作的循环。
While loops
While循环
With the while loop, we can execute a set of statements as long as a condition is true.
使用while循环,只要条件为真,我们就可以执行一组语句。
i = 1while (1 == i)
print("i = 1)
i += 1
This will display ‘i = 1‘ one time.
这将一次显示“ i = 1”。
For Loops
对于循环
A for loop is used for iterating over a sequence.
for循环用于遍历序列。
cars = ["Tesla", "Audi", "Ford"]
for x in cars:
print(x)
功能 (Functions)
A function is a block of code that only runs when it is called.
函数是仅在调用时运行的代码块。
Creating a function
创建一个功能
def my_function():
print("Hello Medium from a function")
Calling the function
调用函数
def my_function():
print("Hello Medium from a function")my_function()
Arguments
争论
def my_function(name):
print("Hello Medium from" + name)my_function("Bryan")
结论 (Conclusion)
Learning a new language can be both daunting and exciting but it’s always worthwhile. With Python having such a dominant presence in the programming world it’s a smart language to dive into, opening up a list of possibilities from progressing your career to having fun with side projects. I hope that this guide has provided a helpful introduction to Python and has helped direct you on your road to mastering the language. Thanks for reading!
学习一种新语言既令人生畏又令人兴奋,但这总是值得的。 Python在编程世界中占有如此重要的地位,因此它是一门精妙的语言,它为您提供了从职业发展到有趣的附带项目的各种可能性。 我希望本指南对Python有所帮助,并帮助您指导您掌握该语言。 谢谢阅读!
翻译自: https://levelup.gitconnected.com/the-ultimate-python-guide-for-beginners-in-2020-ddf736923024
python初学者编程指南