Beginners can understand it at a glance. Python 1

Posted by nocniagenti on Thu, 28 Oct 2021 22:30:44 +0200

Beginners can understand the introduction of python at a glance

First introduce the most basic usage of math. Using math can help you better perform simple calculations, such as square, root, etc.

```import math
math.sqrt(12.0)
dir(math)
```

dir() returns all properties and methods of the specified object without values.
This function will return all properties and methods, even the default built-in properties of all objects.

```from math import cos   # import cos into the namespace
cos(0.4)               # no need to write math.cos
from math import *    # import all functions
sqrt(4.0)
```

Next, let's explain how to judge whether it belongs to different types
Variables are dynamically typed, that is, they do not need to be explicitly declared

```a = 1
type(a)      #result is int
b = 1.0
type(b)      #result is float
b = a
type(b)      #result is int
x = True
x = False
type(x)      #result is Bool
x = 1.0 + 4.0j
type(x)      #result is complex
```

Calculation inevitably uses various operation symbols. The following is a common single operation symbol

```20 + 3, 20 - 3
20 * 3, 20 / 3, 20.0 / 3  # Note, *integer* division
20 % 3, 20.5 % 3.1  # Modulus, i.e. remainder
a = 1; a +=1 ; print(a)
a *= 5; print(a)
```
```4 > 7,  4 < 7, 4 >= 7, 4 <= 7
4 == 7, 4 != 7
```

Logical operation

```4 > 7 and 4 < 7    #result is False
4 > 7 or 4 < 7     #result is True
not 4 > 7          #result is True
```

Compound type: string, list, set, dictionary
character string:

```city = "Sheffield"
type(city)
#result is str
city = 'Sheffield'   # can use " or '
type(city)
#result is str
city[0]  # strings are like lists characters
#result is s
'Jon' + ' ' + 'Barker'  # string concatenation
#result is 'Jon Barker'
city.upper(), city.lower()
help(str)
```

When you are not sure what you should do, use help(str), which will display the help about the class str in the built-in module
For example:
Return self+value.
contains(self, key, /)
Return key in self.
eq(self, value, /)
Return self==value.
format(self, format_spec, /)
Return a formatted version of the string as described by format_spec.
ge(self, value, /)
Return self>=value.
getattribute(self, name, /)
Return getattr(self, name).
getitem(self, key, /)
Return self[key].
A list is similar to a string, but elements can be of any type

List:

```primes = [2, 3, 5, 7, 11]
type(primes)
#result is list
weird = [1, 'Monday', 1.4, False, 4+5j]  # Mixed types
print(weird)
#result is [1, 'Monday', 1.4, False, (4+5j)]
```

Use the data in the list:

```days = ['Mon', 'Tues', 'Weds', 'Thur', 'Fri', 'Sat', 'Sun']
days[0]         #result is 'Mon'
days[1:3]       #result is ['Tues', 'Weds']
days[:]         #result is ['Mon', 'Tues', 'Weds', 'Thur', 'Fri', 'Sat', 'Sun']
days[3:]        #result is ['Thur', 'Fri', 'Sat', 'Sun']
days[::2]       #result is ['Mon', 'Weds', 'Fri', 'Sun']
```

List operations: append, count, extend, index, insert, pop-up, delete, reverse, sort

```days = ['Mon', 'Tues', 'Weds', 'Thur', 'Fri', 'Sat', 'Sun']
days.reverse()
print(days)
days.sort()
print(days)
#result is ['Fri', 'Mon', 'Sat', 'Sun', 'Thur', 'Tues', 'Weds']
x = [1, 2, 3, 4]
x.extend([5, 6, 7])
print(x)
x = list('Let us go then, you and I')
x.count('e'), x.count(' ')
```

Sort here adds that it sorts the Mondays to Sundays in the list in alphabetical order. Sort does not have to be like this. You can also define how you want to sort all the information in the list.
tuples are immutable lists, that is, once created, they cannot be modified.

```x = (1, 2)
type(x)
print(x[0])
#result is 1
```

Parentheses are not absolutely necessary

```pos1 = (10, 20, 30)
pos2 = (10, 25, 30)
pos1 == pos2
#result is False
```

true if all elements are equal

```x, y = 10, 15
x, y = y, x
print(x, y)
#result is 10,15
```

You can swap variables with one line

Entries in the collection must be unique. Sets use curly braces.

```x = {1, 2, 3, 4, 3, 2, 1, 2}
print(x)
#result is {1, 2, 3, 4}
print({1,2,3,7,8, 9}.intersection({1,3,6,7,10}))
print({1,2,3,7,8, 9}.union({1,3,6,7,10}))
print({1,2,3,7,8, 9}.difference({1,3,6,7,10}))
#result is {1, 3, 7}
#          {1, 2, 3, 6, 7, 8, 9, 10}
#          {8, 9, 2}
```

A dictionary maps from a unique key to a value

```office = {'jon': 123, 'jane':146, 'fred':245}
type(office)
#result is dict
office['jon']
#result is 123
office = {'jon': 123, 'jane':146, 'fred':245, 'jon': 354}
print(office['jon'])
#result is 354
```

dict is the abbreviation of dictionaries
The latter item overwrites the previous one, so it defaults to the last value in the dictionary.

Application of dictionary

```office['jose'] = 282  # add a new key-value pair
print(office)
office.keys()
office.values()
'jose' in office  # check if a key exists
```

keys export the names of all entries in the dictionary;
value exports the values corresponding to all entries in the dictionary