Python rookie tutorial learning notes

Python 3 basic syntax

code

The python source file is encoded in UTF-8 by default. All Python strings are Unicode strings, but we can also specify different encoding for the source file. For example:

# -*- coding: cp-1252 -*-

identifier

Almost all languages have their own identifier naming rules, and Python is no exception.
1.Python identifiers must start with an underscore or a letter
2. Other parts of the identifier are composed of letters, numbers and underscores
3.Python is case sensitive

Python reserved word

Reserved words, that is, keywords, are officially reserved by Python and are not provided to users to name any identifiers. You can view all reserved words in Python in the following ways:

import keyword
print(keyword.kwlist)

notes

Python single line comments can be # implemented, while multi line comments can be implemented with three quotation marks' '' or "" (quotation marks should be in English here)

# First comment

'''
This is a multiline comment
 Implemented in three quotation marks
'''

"""
Of course, it can also be implemented in this way
 Not bad
"""

Lines and indents

Unlike Java, C + + and other languages, python uses curly braces {} to distinguish code blocks, but uses indentation. There is no specific requirement for the number of indented spaces, but it must be consistent.

if True:
    print("true")
else:
    print("false")

Incorrect indentation format may cause errors

if True:
    print("Answer")
    print("True")
else:
    print("false")
  print("false")

Multiline statement

Multiline statements can be implemented with backslashes \

item_one = item_two = item_three = 1
total = item_one + \
        item_two + \
        item_three
print(total)

The multiline statements in [], {}, () do not need to use backslashes \, for example:

total = {'item_one', 'item_two', 'item_three',
         'item_four', 'item_five'}

Number type

python has four numeric types: integer, Boolean, floating-point, and complex
Int (integer), such as 1. There is only one integer type int, expressed as a Long integer, without Long in Python 2.
bool (Boolean), such as True.
float (floating point number), such as 1.23, 3E-2
Complex (complex), such as 1 + 2j, 1.1 + 2.2j

character string

  • Single quotation marks and double quotation marks are used exactly the same in python.
  • You can specify a multiline string using three quotation marks ('' or "" "").
  • Escape character\
  • Backslashes can be used to escape, and r can prevent backslashes from escaping.. If r"this is a line with \n", it will be displayed instead of a line break.
  • Concatenate strings literally, such as "this" "is" "string" will be automatically converted to this is string.
  • Strings can be concatenated with the + operator and repeated with the * operator.
  • Strings in Python can be indexed in two ways, starting with 0 from left to right and - 1 from right to left.
  • Strings in Python cannot be changed.
  • Python does not have a separate character type. A character is a string with a length of 1.
  • The syntax format of string interception is as follows: variable [header subscript: tail subscript: step size]
word = 'This is a string'
sentence = 'This is a sentence.'
paragraph = """This is a paragraph,
Can consist of multiple lines of content"""

str = '123456789'

print(str)  # Output string
print(str[0:-1])  # Output the first character to the penultimate character
print(str[0])  # Output first character
print(str[2:5])  # Output the third to fifth characters
print(str[2:])  # Output the third character to the last character
print(str[1:5:2])  # Starting from the second character, output every other character to the fifth character
print(str * 2)  # Repeat the string twice
print(str + 'Hello')  # Connect string and output

print('-----------------------------')

print('hello\nrookie')  # Escape special characters with backslash (\) + n
print(r'hello\nrunoob')  # Add an r before the string to represent the original string without escape

print("\n")  # Line feed
print(r"\n")  # Output \ n

input

The input() function is used in python to get the data entered by the user

text = input("Ask the user to enter information:")

print output

The default output of print is newline. If you want to realize no newline, you need to add end = "" at the end of the variable:

x = "a"
y = "b"
# Wrap output
print(x)
print(y)

print("-----------------------------")
# No newline output
print(x, end="")
print(y, end="")
print()

Import module

  • In python, use import or from... Import to import the corresponding module.
  • Import the whole module in the format of import somemodule
  • Import a function from a module in the format from some module import somefunction
  • Import multiple functions from a module. The format is: from some module import firstfunc, secondfunc, thirdffunc
  • Import all functions in a module in the format from some module import*

Tags: Python

Posted on Sun, 19 Sep 2021 17:27:29 -0400 by siric