Introduction to python Basics

Keywords: Python less Programming C

## Statement: Individual learning arrangement, for reference only

Basic grammar

  • Note: Identify "#", triple double quotation mark ", comment content".

  • Line and indentation: Statements with four spaces hanging indentation and the same block of code must contain the same indentation space! Inconsistent indentation results in grammatical errors in compilation

  • Sentence line feed
string = ("Python It is an object-oriented and interpretive computer programming language."
         "from Guido van Rossum Invented at the end of 1989"
         "The first public release was in 1991."
         "Python The source code also follows GPL(GNU General Public License)Agreement.")
  • Variables and Assignment

## Types: Number Type, Boolean Type, String Type, List and Tuple Type, Dictionary Type

>>> string_one = 'python'  # String type
>>> string_two = 'python'  # String type

>>> list_name = [1, 2, 'hello']  # This is a list.
>>> tuple_name = (1, 2, 'hello')  # This is a tuple

>>> dict_name = {"name":"zhangsan", "age":18}  # This is a dictionary.

# Tip: Lists use "[]", where the number and value of elements can be modified at will; tuples use "()", where elements cannot be modified.

# Tip: Look at the variable type: "type < variable name >"

>>> number = 5.2    # Declare and assign variables
>>> type (number)   # Viewing number Variable Types with type
<class 'float'>     # Display result
  • Identifier (biao`zhi`fu):

# Identifier naming rules:

  1. It consists of letters, underscores and numbers, and cannot begin with numbers.
  2. Case-sensitive
  3. Cannot use keywords

# Identifier naming suggestions: see the name; recommend using uppercase letters for class names, module names with lowercase letters underlined way of naming

  • Numeric Types: Integer, Floating Point, Boolean, Complex

# Tip: Python integer is not affected by the number of digits of the computer, and the range of values is only related to the memory of the computer; as long as the memory is enough, the integer data will be long enough; there will be no problem of data overflow; from Python 2.3, the overflow integer will automatically be converted to long integer;

# Tip: Floating Point Type: Note that the decimal part stores calculation exceptions, and the decimal part will have binary multiplication binary integral infinity (round(x, y)// Let the X decimal part retain y bits, you can get the desired value)

# Various sets of data result exceptions:

# The characteristics of complex type: it is composed of real part and imaginary part, expressed as real + imag J or real + imagJ; real part and imaginary part imag are "floating point type" (Tip: a complex number, must have real number and j representing imaginary part)

>>> a = 1_+2j     # Declare and assign a complex type a variable
>>> a             # The value of output a
(1+2j)            # Output result

>>> a.real        # Output Real Part
1.0               # Output result
>>> type(a.real)  # View the data type of a.real with type
<class 'float'>   # Output a.real data type
>>> a.imag        # Output imaginary part
2.0               # Output result
>>>type(a.imag)   # Viewing the data type of a.imag with type
<class 'float'>   # Data type of output a.imag
  • Data Type Conversion
int(x [,base])        # Convert x to an integer
float(x)              # Converting x to a floating point number
complex(real [,imag]) # Create a plural

# Converting floating-point numbers to integers
>>> a = 1.2
>>> int(a)
1

#Converting integers to floating-point numbers
>>> b = 2
>>> float(b)
2.0

#Create a plural
>>> complex(3.4)
(3.4+0j)

# Operator:

  • Arithmetic operators: add (+), subtract (-), multiply (*), divide (/), take (%), power (**), divide (//) Tip: Self-increasing (++) self-decreasing (--) operators in C language are not supported.
  • Assignment Operators: Allow > > > > x, y = 1, 2
  • Comparing operators: whether equal (==), whether unequal (!=), whether greater than (>), whether less than (<), whether greater than or equal to (>=), whether less than or equal to (<=)
  • Logic Operator: Logic and (and), Logic or (or), Logic Non (not) Tip: Boolean a, B calculation, Logic and, A is false, no calculation of b; Logic or, A is true, no calculation of B
  • Membership operator: used to determine whether the formulation sequence contains a value; in if there is, return true; not in if there is, return false;
a,b = 10,20
list_demo = [1, 2, 3, 4, 5]

# Determine whether variable a is in list_demozhong
if a in list_demo:
    print("1--variable a In a given list list_demo in")
else:
    print("1--variable a Not in the given list list_demo in")

# Determine whether b is in list_demo
if b not in list_demo:
    print("2--variable b Not in the given list list_demo in")
else:
    print("2--variable b In a given list list_demo in")

# Modify the value of variable a
a = 2
if a in list_demo:
    print("3--variable a In a given list list_demo in")
else:
    print("3--variable a Not in the given list list_demo in")

# Bit operation: left shift by bit (<), right shift by bit (>), and (&), by bit or (|), by bit exclusive or (^), by bit inverse (~)

  • Bit-by-bit left shift (<<): Binary bit, symbol bit unchanged, whole left shift n bit, high bit discarded, low bit complemented 0; removal bit deleted, carry complemented 0
  • Bit-by-bit right shift (<<): Binary bit, symbol bit unchanged, whole right shift n bit, high bit complement 0, low bit discard; remove bit delete, move bit complement 0
  • Bitwise and (&): binary, bitwise logic and operation
  • Bit-by-bit or (|): Binary, bit-by-bit logic or operation
  • Bit exclusive OR (^): Binary, logical exclusive OR operation by bit
  • Bit-by-bit inversion (~): binary, bit-by-bit inversion, that is: source code - > inverse code

Posted by ljschrenk on Tue, 10 Sep 2019 20:55:13 -0700