Python study notes - variables and simple data types

1. What happens when you run Hello_word.py

      1 >Hello_word.py indicates that this is a Python program, and the editor will use the interpreter to run it .

       2>The python compiler has syntax highlighting.

2. Variables 

  1>                            
message = "Hello python world!"
print(message)
message="Hello python crash course world!"
print(message)
output:
Hello python world!
Hello python crash course world!

Note: The variable value can be modified at any time in the program, and Python will always record the latest value of the variable.

2> a> variable name (letters, numbers, underscores) Note: Numbers cannot start

             b>The variable name cannot contain spaces

             c> cannot use Python keywords and function names as variable names

             d> Variable names should be short and descriptive

             e> Use lowercase i and uppercase O with caution

            Note: It is best to use lowercase letters for variable names

3. String

    1> A string is a series of characters, all strings are enclosed in quotation marks, and the quotation marks can be single or double quotation marks.

     2>

name="ada lovelace"
print(name.title())
 output:
Ada Lovelace

    // methods usually require extra information to do the job

    //Methods are operations that Python can perform on data

    //Let Python perform the operation specified by the method title() on the variable name

/************************************************************************************************************

title() displays each word in uppercase

upper() changes all letters to uppercase

lower() changes all letters to lowercase

**************************************************************************************************************/

    3> Splicing

first_name="ada"
last_name="lovelace"
full_name=first_name+" "+last_name
print(full_name)

output:

ada lovelace
print("Hello,"+full_name.title()+"!")
output:
Hello, Ada Lovelace!

//You can use concatenation to create a message and store the message in a variable


    4>Blank generally refers to any non-printing characters, such as spaces, tabs, and newlines

    >>>print("python")

     python

    >>>print("\tpython")

            python

/**********************************************************************************************************************

Tab character \t

newline character \n

"\n\t" tells python to wrap to the next line and add a tab at the beginning of the next line

********************************************************************************************************************/


    5> //The method rstrip() ensures that there is no blank at the end of the string (only temporarily) To permanently remove blanks from the string, the result of the delete operation must be returned to the variable

            //The method lstrip() removes the blanks at the beginning of the string

            //The method strip() removes the blanks at both ends of the string

             例:>>>favorite_language.rstrip()


    6> If the string enclosed in single brackets contains apostrophes, it will cause an error


    7> In python2, there is no need to put the content to be printed in parentheses, while in python3 print is a function, and parentheses are essential.   

message="Happy"+str(age)+"rd Birthday"


4.  

         1> integer

            In python, (+)(-)(*)(/) operations can be performed on integers

        2> floating point number

            The number of decimal places the result contains may be indeterminate

        3>             

age=23
message="Happy"+age+"rd Birthday"
print(message)

    Type error. //age is an int type, it is impossible to determine whether it is the value 23 or the characters 2 and 3

Amendment:

message="Happy"+str(age)+"rd Birthday"



            

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324408897&siteId=291194637