Introduction to Nursery Pthon - Variables, Characters

What is a variable? Before learning variables, let's look at some of the things you need to know:

a = 1
b = 2
c = a + b
print(c)
 
print(1+2)
Copy Code

Here we implement an addition operation. It is also possible to output 1+2 directly with the output statement print, but if we use variables, then one variable equals 1 and the other variable equals 2, which you can call assignment. This means giving a value to a variable.

So a, B and c are variables here, and 1 and 2 are the values we give to the variables, giving them a value. c is the medium we use to implement a+b, and of course you can print directly (a+b). They are written differently and get the same result.

Variables also have their own temper to see what they don't allow: valid naming invalid naming

china	china-t(No dashes allowed)
chinaHu	china hu(No spaces allowed)
china_hu	1china(Number start is not allowed)
_china	china$(Special characters are not allowed)
CHINA	'china'(String form is not allowed)
china9	
Copy Code

If this form doesn't make sense, what about this?

abc_xyz = 'Hello Python'                #Legal.
HelloWorld = 'Hello Python'           #Legal.
abc = 'Hello Python'                       #Legal.
xyz#abc = 'Hello Python'               #"Not legal, not allowed in identifier"#"Number.
abc1 = 'Hello Python'                   #Legal.
1abc = 'Hello Python'                   #Illegal, identifier does not allow number start.
w b = 'Hello Python'                     #Illegal, with spaces in between

    The quotation marks above are strings, which will be explained in the next chapter, so you can understand them a little.
Copy Code

Be careful:

1,Whether it's a variable name or a file name, try to keep it short or descriptive for yourself or others to see.
    For example: name than n Good number, first_name than f_n good
2,And don't put Python Use the keyword or function name as the variable name
    For example, the most obvious is print,class etc.
3,Try not to use lowercase letters l With capital letters O,I might get confused one day and think of it as a number 1 or 0
4,Note the Chinese and English characters, especially  "  ''These two strings

Character string

         What is a string? Important? The answer is yes, it's so important that you need it later, so go ahead and learn.     

    ```
    name = 'Hello World'
    print(name)
    ```
Copy Code

         Here'Hello World'is what we call a string. Name is its variable. A variable name equals a string followed by a single quotation mark and double quotation marks. Whether it's a Chinese character, a letter, or a number, it's a string.

      Notes for single and double quotation marks:

```
    # Correct Writing
    name = '"There is nothing else to do in Qing An"'
    # Wrong Writing
    name1 = ''There is nothing else to do in Qing An''
    ```
Copy Code

         Let's make some changes to the string:

.title()

    ```
    name = 'hello python'
    print(name.title())
    ```
Copy Code

         Here we get a string with the first letter capitalized:

Hello Python
 Copy Code

.upper()

    ```
    name = 'hello python'
    print(name.upper())
    ```
Copy Code

        Here we get a string with all uppercase letters:

    ```
    HELLO PYTHON
    ```
Copy Code

.lower() method

         Here our strings are guaranteed to be all or part uppercase first, otherwise they will print out exactly the same as the strings we write in!

    ```
    name = 'HELLO PYTHON'
    print(name.lower())
    ```
Copy Code

         Here we get a string with all lowercase letters:

hello python
 Copy Code

strip() method

         Remove spaces: Note: Tab \t can also be spaces: print('\tpython')

    ```
    name = 'python '
    name = ' python'
    name = ' python '
    # Remove trailing spaces
    print(name.rstrip())
    # Remove leading spaces
    print(name.lstrip())
    # Delete spaces on both sides
    print(name.strip())
    ```
Copy Code

         The result is:

    ```
    python
    python
    python
    ```
Copy Code

         Next, let's use some variables in the string to see an example:

    first_name = 'Hello'
    last_name = 'World'
    # f is the meaning of a string, short for format, is a format
    #Many errors can be avoided
    print(f"{first_name}{last_name}")


    # You can also write that
    first_name = 'Hello'
    last_name = 'World'
    # Given a variable,
    full_name = f"{first_name}{last_name}" # Here the first two defined variables are assigned to the new variable
    # Print by a new variable, and print as described above
    print(full_name)
    ```

        Strings are also useful for many purposes: can you avoid a lot of errors!!!

Copy Code
    first_name = 'Qing An'
    last_name = 'Nothing else'
    # You can output some Chinese characters or English numbers directly in it.
    print(f"1,My name is:{first_name}{last_name}!")
    ```
Copy Code

        The result of these three examples is:

    ```
    Hello World
    Hello World
    There is nothing else to do in Qing An
    ```
Copy Code

It's still interesting to look closely, so let's take a look at the value.

clearsecuritynothingothermatter
positive sequence01234
Reverse order-5-4-3-2-1

Name ='nothing else in peace'--> print (name[:3])

Print (name [2:4]) --> take value: no difference      # positive sequence

Print (name[-3:-1]) --> take value: no difference      # Reverse Order

print(name[-2:]) -->Value: nothing else      # Reverse Order

Print (name [0:5:2]) -->Value: Nothing to do      # Interval Value

Pthon Ultra-Full Database Installation Package Learning Route Project Source Free Sharing

Tags: Python Back-end

Posted on Mon, 08 Nov 2021 14:54:22 -0500 by hairyjim