Python的基础,Python的101(Python foundation, python 101)

  近年来,Python已经成为全世界最流行的编程语言之一。它是一种高级通用编程语言.

  本文将重点介绍对所有初学者Python程序员都很重要的基本语法、数据类型、控制流等概念。

  Python是一种流行的编程语言的原因Python由于其简化的语法而易于学习和使用。Python有一个很大的社区,在那里新开发人员可以在他们的职业生涯中成长和成熟。Python是跨平台的,因此它可以在任何类型的环境中使用,并且仍然产生最大的结果。Python编程的应用

  Python有大量模块、包和框架,这些模块、包和框架允许应用于以下领域:

  Web开发机器学习与人工智能数据科学自动化游戏开发Python编程基础空白与压痕

  Python代码的一般结构主要由空格和缩进组成,与其他主要使用分号和大括号的语言不同。让我们来看看一个例子:

  if __name__==”__main__”:

  print(“Hello Python programmers!”)

  根据上面的代码片段,我们可以得出这样的结论:压痕和白空间使程序员更容易了解代码块从哪里开始和结束的位置,从而提高代码的可读性。

  评论意见

  Python编程还涉及使用评论意见,它们是在程序中编写的语句,用于解释特定行或代码块的用途。Python解释器忽略了它们。下面是一个评论的例子:

  #displaying a child’s age

  标识符

  Python语法的另一个重要部分是标识符。这些单词用于命名对象,如变量、函数和类。Python标识符以字母或下划线(_)开头。后续字符可以是下划线或字母数字字符。例如,班主任可以是标识符,但是2学生不能用作标识符。

  关键词

  Python还有几个关键词,它们基本上是具有特殊意义的词,可用于执行特定的任务。它们不应用作标识符。它们包括:

  if

  else

  True变量

  变量是值得存储位置。声明如下:

  name=”John”

  print(name)

  变量“name”存储值“john”。在命名变量时,我们应该注意,它们不能以数字开头,也不能包含空格。例如,Num_1可以是变量名,但是Num 1不能是变量名。变量的名称应尽可能简洁和描述。

  Python中的数据类型(一)字符串

  字符串是一系列字符。Python中的字符串通常用引号括起来。例如:

  city=’New York’

  country=”Kenya”

  上面的声明是单行字符串.Python还允许多行字符串,其中我们使用三种引号:

  customer1=”’Name: John Doe

  ID: 224

  Address: Summer Beach House, 110”’

  print(customer1)(二)数字

  在Python编程中,支持各种类型的数字,例如整数 , 浮子和复数。整数是像1,3和45这样的数字。浮点数是小数点的数字。它们也被称为浮点数.数字声明示例如下所示:

  num1=20

  num2=5.5

  num3=complex(5, 6)

  从上面的例子中,num 1被指定为整数,num 2被分配为浮点数,num 3被分配为复数。

  (三)布尔数据类型

  在Python中,布尔数据类型通常表示真或假。它通常用于检查一个条件是否为真,并根据结果执行特定的操作。

  例如:

  is_allowed=True

  is_full=False常数

  有时,在编写代码时,我们可能需要将值存储在变量中,并且希望不更改值。Python不允许我们直接声明常量,但是我们可以用大写字母声明变量,以区别于可能不是常量的其他值。下面是一个例子:

  TIME_LIMIT=2Python中的运算符(一)赋值运算符

  它用于为变量赋值。下面是一个例子:

  basic_salary=100000

  其他例子包括+= , -= , /=等。

  (二)算术运算符

  在Python中,我们有作为用户的操作符来执行算术运算,如加法、减法、乘法和除法。下面的代码显示了其中一些操作:

  a=55

  b=5

  c=a + b # Addition

  d=a – b # Subtraction

  e=a * b # Multiplication

  f=a / b # Division

  print(“55 + 5=” + c)

  print(“55 – 5=” + d)

  print(“55 * 5=” + e)

  print(“55 / 5=” + f)(Iii)关系算子

  这些也称为比较运算符,用于比较值。例如:

  25 < 30

  5==”a”

  第一个语句将返回True,而第二个语句将返回false。

  (四)逻辑运算符

  它们用于同时检查多个条件。它们包括“和”、“或”和“不”运算符。例如:

  age=24

  age < 40 and age > 5

  表达式返回True,因为这两个条件都是true。

  (五)成员算子

  其中包括“in”和“not in”操作符。例如:

  4 in [1, 2, 3, 4, 5, 6]

  该语句返回True,因为列表中有4。

  (六)身份操作者

  他们检查任何两个值是否相同。例如:

  4 is 4.000

  表达式的计算结果为false。

  (七)位运算符

  这些操作符一点一点地操作任何给定的值。它们包括按位和 , 按位或等。

  Python中的控制流(I)IF陈述

  If语句用于根据特定条件执行代码块。如果条件为真,则执行代码块。否则,它将被忽略。

  (二)如果.其他陈述

  当我们希望在条件为真时执行特定的操作时,使用此方法;当条件变为false时,则使用不同的操作。

  (三)如果.Elif.其他陈述

  这用于检查两个以上的条件,并根据它们的真假执行不同的操作。下面是一个简短的例子:

  subject=input(“Enter the subject you wish to register: )

  if subject==”Mathematics”:

  print(“Available”)

  elif subject==”Physics”:

  print(“Check next week”)

  else:

  print(“Unavailable”)

  (Iv)for循环

  当我们需要执行特定次数的代码块时,就会使用for循环。

  语法如下:

  for index in range(r):

  statement(V)同时循环

  这种结构允许我们执行一个代码块,只要条件仍然为真。语法方面的一个例子是:

  while x < 10:

  statement

  x +=1

  总之,这些是我们需要理解和掌握的最基本的概念,我们要成为优秀的开发人员。编码愉快!

————————

In recent years, python has become one of the most popular programming languages in the world. It is a high – level general – purpose programming language

This article will focus on the concepts of basic syntax, data type, control flow and so on, which are important to all beginners Python programmers.

Python is a popular programming language because Python is easy to learn and use because of its simplified syntax. Python has a large community where new developers can grow and mature in their careers. Python is cross platform, so it can be used in any type of environment and still produce the most results. Application of Python Programming

Python has a large number of modules, packages, and frameworks that allow applications in the following areas:

Web development machine learning and artificial intelligence data science automation game development python programming foundation blank and indentation

The general structure of Python code is mainly composed of spaces and indents, which is different from other languages that mainly use semicolons and braces. Let’s take a look at an example:

if __ name__== “__main__”:

  print(“Hello Python programmers!”)

From the above code snippets, we can conclude that indentation and white space make it easier for programmers to understand where code blocks start and end, so as to improve the readability of the code.

Comments

Python programming also involves the use of comments, which are statements written in a program to explain the purpose of a particular line or block of code. The Python interpreter ignores them. Here is an example of a comment:

  #displaying a child’s age

Identifier

Another important part of Python syntax is identifiers. These words are used to name objects such as variables, functions, and classes. Python identifiers are written in letters or underscores () start. Subsequent characters can be underscores or alphanumeric characters. For example, a head teacher can be an identifier, but 2 students cannot be used as identifiers.

Key words

Python also has several keywords, which are basically words with special meaning and can be used to perform specific tasks. They should not be used as identifiers. They include:

if

else

True variable

Variables are worth storing. The statement is as follows:

name=”John”

  print(name)

The variable “name” stores the value “John”. When naming variables, we should note that they cannot start with numbers or contain spaces. For example, num_ 1 can be a variable name, but num 1 cannot be a variable name. The names of variables should be as concise and descriptive as possible.

Data type (I) string in Python

A string is a series of characters. Strings in Python are usually enclosed in quotation marks. For example:

  city=’New York’

  country=”Kenya”

The above declaration is a single line string Python also allows multiline strings, where we use three kinds of quotation marks:

  customer1=”’Name: John Doe

ID: 224

  Address: Summer Beach House, 110”’

Print (customer1) (II) number

In Python programming, various types of numbers are supported, such as integers, floats and complex numbers. Integers are numbers like 1, 3, and 45. Floating point numbers are numbers with a decimal point. They are also called floating point numbers An example of a digital declaration is as follows:

num1=20

num2=5.5

num3=complex(5, 6)

From the above example, Num 1 is specified as an integer, Num 2 is assigned as a floating point number, and num 3 is assigned as a complex number.

(III) Boolean data type

In Python, Boolean data types usually represent true or false. It is usually used to check whether a condition is true and perform specific operations based on the result.

For example:

  is_allowed=True

is_ Full = false constant

Sometimes, when writing code, we may need to store the value in a variable and want to not change the value. Python does not allow us to declare constants directly, but we can declare variables in uppercase letters to distinguish them from other values that may not be constants. Here is an example:

TIME_ Limit = operator in python (I) assignment operator

It is used to assign values to variables. Here is an example:

  basic_salary=100000

Other examples include + =, – =, / =, etc.

(II) arithmetic operators

In Python, we have operators as users to perform arithmetic operations, such as addition, subtraction, multiplication and division. The following code shows some of these actions:

a=55

b=5

  c=a + b # Addition

  d=a – b # Subtraction

  e=a * b # Multiplication

  f=a / b # Division

print(“55 + 5=” + c)

print(“55 – 5=” + d)

print(“55 * 5=” + e)

Print (“55 / 5 =” + F) (III) relation operator

These are also called comparison operators and are used to compare values. For example:

25 < thirty

5==”a”

The first statement will return true and the second statement will return false.

(IV) logical operators

They are used to check multiple conditions at the same time. They include the and, or, and no operators. For example:

age=24

  age < 40 and age > 5

The expression returns true because both conditions are true.

(V) member operator

These include the in and not in operators. For example:

4 in [1, 2, 3, 4, 5, 6]

The statement returns true because there are 4 in the list.

(VI) identity operator

They check whether any two values are the same. For example:

4 is 4.000

The expression evaluates to false.

(VII) bitwise operators

These operators operate on any given value bit by bit. They include bitwise and, bitwise OR, etc.

Control flow (I) if statement in Python

The if statement is used to execute a block of code based on specific conditions. If the condition is true, the code block is executed. Otherwise, it will be ignored.

(II) if Other statements

Use this method when we want to perform a specific operation when the condition is true; When the condition becomes false, different operations are used.

(III) if Elif. Other statements

This is used to check more than two conditions and perform different operations according to their true and false. Here is a short example:

  subject=input(“Enter the subject you wish to register: )

  if subject==”Mathematics”:

  print(“Available”)

  elif subject==”Physics”:

  print(“Check next week”)

else:

  print(“Unavailable”)

(IV) for loop

When we need to execute a block of code a certain number of times, we use the for loop.

The syntax is as follows:

  for index in range(r):

Statement (V) simultaneous cycle

This structure allows us to execute a block of code as long as the condition is still true. An example of grammar is:

while x < 10:

  statement

x +=1

In short, these are the most basic concepts we need to understand and master. We want to become excellent developers. Happy coding!