[Python] Getting Started Lesson 1: Understand basic syntax (data types)

Table of contents

 1. Introduction

1. What is python?

2. Several characteristics of python

2. Examples

1. Comments

2. Data type

2.1. String str

2.2. Integer int

2.3. Floating point number float

2.4. Boolean bool

2.5. List list

2.6. Tuple

2.7. Set

2.8. Dictionary dict


 1. Introduction

1. What is python?

Python is a general-purpose high-level programming language created by Guido van Rossum in 1991. It is designed to be easy to read, easy to learn, and has a clear and concise grammatical structure. Python supports multiple programming paradigms, including object-oriented, functional, and procedural programming. It has a wide range of third-party libraries and modules that can be used for application development in various fields, such as website development, scientific computing, artificial intelligence, etc. This language is widely used in various fields and is very popular in the fields of data science and machine learning.

2. Several characteristics of python

  1. Easy to learn: Python's syntax is concise and clear, and it is highly readable, allowing beginners to quickly get started with programming.
  2. Object-oriented: Python supports object-oriented programming, can use classes and objects to organize and manage code, and provides features such as encapsulation, inheritance, and polymorphism.
  3. High development efficiency: Python has a wealth of built-in libraries and third-party libraries, which greatly speeds up the development process. At the same time, Python also has functions such as documentation and debugging tools to improve development efficiency.
  4. Cross-platform: Python is a cross-platform programming language that can run on almost all operating systems, such as Windows, Mac, and Linux.
  5. Powerful ecosystem: Python has a large and active community, providing a variety of libraries and tools, such as NumPy, Pandas, Scikit-learn, and Django, for use in different fields such as data analysis, machine learning, and web development. application.

Python is a simple, easy-to-learn, and powerful programming language that is suitable for various application development and has significant advantages in development efficiency and ecosystem.

2. Examples

1. Comments

Annotations are divided into three types of annotations:

  1. #
  2. '''     '''
  3. """     """
# 注释1

''' 注释2 '''
"""
注释3
"""

2. Data type

Python is a dynamic and strongly typed language.

Dynamic/Static In statically typed languages , type checking occurs at compile time . In dynamically typed languages , type checking occurs at run time .

  1. Static typing (static) : All variable types must be explicitly declared, because this information is required during the compilation stage.
  2. Dynamic : Explicit declaration is not required because type assignment occurs at runtime.

Strong typing/weak typing In strong typing, whether at compile time or run time, once a type is assigned to a variable, it will hold this type and cannot be mixed with other types when calculating an expression. In a weak type, it is easy to mix calculations with other types.

2.1. String str

In Python , everything enclosed in quotation marks is a string, and the quotation marks can be single quotation marks or double quotation marks .

name= 'hello world'
name = "hello world"

You can use + to concatenate strings

print("hello" + " world")

You can also use the tab character \t and the newline character \n

2.2. Integer int

In Python, addition (+) subtraction (-) multiplication (*) and division (/) operations can be performed on integers.

count = 9
print(count + 1)
print(count - 1)
print(count * 2)
print(count / 2)

Python uses two multiplication signs to represent exponentiation operations.

print(count ** 2) #81

2.3. Floating point number float

Python calls numbers with decimal points floating point numbers.

a = 0.1
b = 0.2

Note, however, that the number of decimal places contained in the result may be undefined. Computer conversion problem, this is related to your computer

print(a + b)

2.4. Boolean bool

  • True
  • False

>= <= == and or

# 短路  and  or
print(1 and 2 and 3 and 4)
print(1 and 2 and 0 and 4)

2.5. List list

In Python, square brackets [ ] are used to represent lists, and commas are used to separate elements.

l1 = [1, 3, 4, 2, 5]

The reading method is consistent with other languages : because it is ordered , the value can be obtained through the index. Indexes start at 0, but can be negative.

# 读取内容
print(l1[0])
# 下标可以是倒序的
print(l1[-1])
# 改变值
l1[-1] = 6
# 遍历
print(l1)

Add content to the stack: push, pop, and take

  • pop : get elements (delete elements)
  • extend : add
  • copy : copy the new reference
  • append : add
  • l1 .sort() : Sort
  • l1 .sort(reverse=True) : reverse order
  • remove : delete an element
  • insert : insert
  • clear : clear

Python also supports some methods to operate on it.

  • Add element append at the end of the list
l1.append("blue_bear")
  • insert element into list insert
l1.insert(0, "blue_bear")

Remove element from list

  • pop |delete based on index
l1.pop()
l1.pop(0)
  • removeDelete based on element
bicycles.remove('cannonade')

The method remove() only removes the first specified value. If the value you want to delete may appear multiple times in the list, you need to use a loop to determine whether all such values ​​have been deleted.

  • Sort a list permanently
bicycles.sort()

It is also possible to arrange the list elements in reverse alphabetical order, to do this simply pass the parameter reverse=True to the method.

  • Temporarily sort the list sorted
sorted(bicycles)
  • Reverse the order of list elements reverse
  • Determine the length of the list len
len(bicycles)

Python also provides slicing functions for types such as lists

Slicing format variable name [start:stop:step] start: start subscript stop: stop subscript step: step length

  • The positive values ​​​​of start and stop represent the list subscript, and the negative values ​​​​represent the last number of data in the list from left to right.
  • The direction is determined by step . When step is positive, slice from left to right. When step is negative, slice from right to left.
  • The empty values ​​of start and stop represent the last data at the head and tail of the list respectively . As for the empty values ​​of start and stop, whether they represent the head or the tail of the list is determined by the positive and negative values ​​​​of step, that is, the step determines the direction of the list slice. Decision later.

2.6. Tuple

Tuples are similar to lists, but are identified using parentheses instead of square brackets, and the internal elements and size cannot be changed. Quick assignment of tuple elements.

nums = (0, 1, 2)
a, b, c = nums

2.7. Set

It is an unordered sequence of non-repeating elements that can be created using the set() method and { } .

cities = set()

cities = {'hunan', 'shanghai', 'beijing'}
  • add elementadd
  • Deleting elements remove will report an error, discard will not report an error, pop, clear
  • Determine whether element exists a in b

Set operations

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a - b)
print(a | b)
print(a & b)
print(a ^ b)

2.8. Dictionary dict

In Python, a dictionary is represented by a series of key-value pairs enclosed in curly braces {} . Similar to json

A dictionary is a collection of key-value pairs. Each key is associated with a value, and you can use the key to access the value associated with it. The values ​​associated with keys can be numbers, strings, lists, or even dictionaries.

alien = {'color': 'green', 'points': 5}

To get the value associated with a key, specify the dictionary name followed by the key in square brackets

alien['color']

A dictionary is a dynamic structure in which key-value pairs can be added at any time. To add a key-value pair, specify the dictionary name, the key enclosed in square brackets, and the associated value.

alien['name'] = 'jack'

For information that is no longer needed in the dictionary, you can use the del statement to completely delete the corresponding key-value pair. When using the del statement, you must specify the dictionary name and the key to be deleted.

del alien['name']

Guess you like

Origin blog.csdn.net/weixin_74383330/article/details/133085250