首页  >  文章  >  后端开发  >  Python的基本概念是什么?

Python的基本概念是什么?

王林
王林转载
2023-08-31 22:09:101416浏览

Python的基本概念是什么?

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.

Python的特点

以下是Python的主要特点:

  • Python支持函数式和结构化编程方法以及面向对象编程。

  • It can be used as a scripting language or can be compiled to byte-code for building large applications.

  • It provides very high-level dynamic data types and supports dynamic type checking.

  • It supports automatic garbage collection.

Variables in Python

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Let’s create a variable.

a = 10

上面,a是一个变量,赋值为整数10。

Numeric Datatype in Python

Number data types store numeric values. They are immutable data types, means that changing the value of a number data type results in a newly allocated object.

Python supports four different numerical types.

  • int (signed integers) − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.

  • long (long integers ) − Also called longs, they are integers of unlimited size, written like integers and followed by an uppercase or lowercase L.

  • float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

  • complex (complex numbers) − are of the form a + bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number). The real part of the number is a, and the imaginary part is b. Complex numbers are not used much in Python programming.

Strings in Python

字符串是Python中最受欢迎的类型之一。我们可以通过在引号中包含字符来简单地创建它们。Python将单引号和双引号视为相同。创建字符串就像将一个值赋给一个变量一样简单。

Let’s see how to easily create a String in Python.

myStr = Thisisit!'

Lists in Python

列表是Python中最多功能的数据类型,可以用方括号将逗号分隔的值(项)写成列表。让我们看看如何创建不同类型的列表。

myList1 = ['abc', 'pq'];
myList2 = [5, 10, 15, 20];

Tuples in Python

Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. Let’s see how to create a Tuple.

myTuple1 = ('abc', 'pq)];
myTuple2 = (5, 10, 15, 20);

Dictionary in Python

字典是Python中的一种序列。在字典中,每个键都与其值用冒号(:)分隔,项之间用逗号分隔,并且整个字典被括在花括号中。字典中的键是唯一的,而值可能不是唯一的。字典的值可以是任何类型,但键必须是不可变的数据类型,例如字符串、数字或元组。

Let’s see how to create a Dictionary −

# Creating two Dictionaries
dict1 = {'Player':['Jacob','Steve','David','John','Kane'], 'Age':[29, 25, 31, 26, 27]}
dict2 = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]}

Classes & Objects in Python

类是一个用户定义的对象原型,它定义了一组属性,这些属性描述了该类的任何对象。这些属性是数据成员和方法,通过点符号访问。

An object is a unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.

Functions in Python

function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

函数块以关键字 def 开始,后跟函数名和括号 ( ( ) )。让我们创建一个函数。

def demo(s):
   print (s)
   return
# Function call
demo("Function Called")

输出

Function Called

以上是Python的基本概念是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除