Basic syntax for getting started with python

Keywords: Python

Basic grammar

·Single line note

Start with #

example:

#Output Picchu

print ("Pikachu")

·Multiline comment

Start with three quotation marks and end with three quotation marks

"""

"""

example

"""

output

"""

·Identifiers and keywords

Identifier: alphanumeric and underscore. It can only start with a letter or underscore

variable

  • Object has type

  • Variables are untyped, and variables are just references

a=10
print(a=10)

Operation results:

Traceback (most recent call last):
File "D:/PycharmProjects/pythonprj1/test2.py", line 5, in
print(a=10)
TypeError: 'a' is an invalid keyword argument for print()

Process finished with exit code 1

a=10
print(a==10)

Operation results

True

  • Integers and strings are cached in python,

Numbers and strings in python have been created and cannot be modified

Comparison of variables

"= =" and "is"

• comparison of variables "= =" and "is"

• = =: whether the values of the data stored by the comparison object are equal;

• is: compare whether both variables refer to the same object.

data type

  • Number type numbers

    Integer int (including Boolean bool)

    Floating point float

    complex type

  • String type str

  • List type list

  • Tuple type tuple

  • Set type set

  • Dictionary type dict

integer

#Express binary 0b or 0b
a=0b110
print(a)

b=(0B1010)
print(b)

#Express octal 0o or 0o
c=0o123
print(c)

#Express hex 0x or 0x
d=0xABC
print(d)

#Execute the shortcut key shift+F10 now
  • [the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-uap0oxdt-1631108974850) (C: \ users \ administrator \ appdata \ roaming \ typora \ typora user images \ image-20210907085437406. PNG)]

    • binary, preceded by: 0b or 0b (composed of 1 and 0, enter 1 every 2);

    • octal, preceded by: 0o or 0o; (composed of 0 ~ 7, 1 for every 8)

    • hexadecimal, preceded by: 0x or 0x. (09. AF composition, 1 in every 16)

a=10
#bin(), oct(),hex() are built-in functions
# Decimal to binary
b=bin(a)
#Decimal to octal
c=oct(a)
#Decimal to hexadecimal
d=hex(a)
print(a,b,c,d)

Operation results

10 0b1010 0o12 0xa

#Take a part of it from the second index position to the end, and the name is "slice"
print(a,b[2:],c[2:],d[2:])

Operation results:

10 1010 12 a

#Can calculate and output large numbers
intData=2013477777777777777777777777777777777777777777777777777777777777776452754264265625626572
#"m**n" is the nth power of m
intData=2**111
print(intData)

Operation results:

27817953874931422193200157252797783625799553985162128378849682836025612139606873061796642986409084093027488282529479441356879309912545136580926563006055687622422043646784287399491305767249266136610482504757705065498211196461322151021271625793951908619828755931791767521474322440333549820076334848895352132541368625047123052020050690048

float

f1=1.23828475
print(f1)
#int() can be used to represent the integer part, and will not be rounded, truncated and rounded. If you want to round and add 0.5
d1=int(f1)
print(d1)

Operation results:

1.23828475
1

#Integer to decimal 
f2=float(d1)
print(f2)

Operation results

1.0

Plural form

#Complex complex
c1=1+2j
print(c1)

c2=complex(1,1)
print(c2)

Operation results:

(1+2j)
(1+1j)

#Addition, subtraction, multiplication and division of complex numbers
print(c1+c2)
print(c1-c2)
print(c1*c2)
print(c1/c2)

Operation results

(2+3j)
1j
(-1+3j)
(1.5+0.5j)

#Print real part and imaginary part
print(c1.real)
print(c1.imag)
#Print conjugate complex
print(c1.conjugate())

Operation results

1.0
2.0
(1-2j)

#
c3=-1j
print(c3)

c4=1j
print(c4)

Operation results

(-0-1j)
1j

b1=True
b2=False
print(b1,b2)

#True defaults to 1 and FALSE defaults to 0
b3=b1+1
print(b3)

Operation results

True False
2

#Bool - > int - > float - > complex conversion
print(1+False)
print(False-1.273+100)
print(False+True+100+1+2j)

Operation results

(102+2j)

Each python object has a Boolean value**

#bool

print(bool(None))
#integer
print(bool(0))
#float 
print(bool(0.0))
#Plural form
print(bool(0+0j))
#Boolean
print(bool(False))
#Empty list
print(bool([]))
#An empty dictionary can only express an empty data
print(bool({}))
#tuple
print(bool(()))
#Empty set
print(bool(''))
print(bool(""))

Operation results

False
False
False
False
False
False
False
False
False
False

• built in function type(): detect different types of data.

• built in function isinstance(): judge whether the data belongs to a certain type.

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-gva5ro6b-1631108974852) (C: \ users \ administrator \ appdata \ roaming \ typora \ typora user images \ image-20210907095042434. PNG)]

, FALSE, the default is 0
b3=b1+1
print(b3)

Operation results

True False
2

#Bool - > int - > float - > complex conversion
print(1+False)
print(False-1.273+100)
print(False+True+100+1+2j)

Operation results

(102+2j)

**every last python Objects have a Boolean value****

#bool

print(bool(None))
#Integer
print(bool(0))
#Floating point type
print(bool(0.0))
#Plural form
print(bool(0+0j))
#Boolean
print(bool(False))
#Empty list
print(bool([]))
#An empty dictionary can only express an empty data
print(bool({}))
#Tuple
print(bool(()))
#Empty set
print(bool(''))
print(bool(""))

Operation results

False
False
False
False
False
False
False
False
False
False

•Built in function type(): For different types of data**testing**. 

•Built in function isinstance(): **judge**Whether the data belongs to a certain type.

[External chain picture transfer...(img-gVA5Ro6b-1631108974852)]

Posted by Daniel0 on Wed, 08 Sep 2021 11:35:23 -0700