程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python notes 01

編輯:Python

python

python Output function print Escape character and original character binary and character encoding python The definition of identifier and reserved word variables in and common data type data type conversion using multiple assignment of variables Python The comments in

Output function print

  • print It can output numbers

print(520)
print(98.5)
  • ptint You can output strings

print('helloworld')
print("helloworld")
  • Expressions with operators , Expression results can be evaluated

print(3+1)
  • Output data to a file

# Be careful :1. The specified drive letter must exist 2. To use file=fp
fp=open("D:/text.txt", "a+")#a+: If the file doesn't exist, create , If it exists, continue to add... After the contents of the file
print('helloworld', file=fp)
fp.close()
  • No line feed output

# Split with commas
print('hello','world','python')

Escape character and original character

# Escape character
print('hello\nworld') #\+ The first letter of the escape function   n-->newline The first letter of indicates a line break
print('hello\tworld') #\+ The first letter of the escape function   t-->tab The first letter of indicates the tab character
print('helloooo\tworld')
print('hello\rworld')#\+ The first letter of the escape function   r-->return The first letter of "enter" means "enter" # world take hello To cover
print('hello\bworld')#\+ The first letter of the escape function   b-->back The first letter of indicates backspace
print('http:\\\\www.baidu.com')
print(' The teacher said :\' Hello everyone \'')
# Original character : You don't want escape characters in strings to work , Use the original character , Is to add... Before the string r or R
print(r'hello\nworld')
# Be careful The last character cannot be a backslash print(r'hello\nworld\')

Binary and character encoding

print(chr(0b100111001011000))
print(ord(' ride '))

python Identifiers and reserved words in

  • Reserved words

import keyword
print(keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

  • identifier

Variable 、 function 、 class 、 The names of modules and other objects are called identifiers

- Alphanumeric underline
- Cannot start with a number
- It can't be reserved
- Case sensitive 

Definition and use of variables

  • A variable is a labeled box in memory

name = ' Mario '
print(name)
print(' identification ',id(name))
print(' type ',type(name))
print(' value ',name)

Multiple assignment of variables

name=' Mario '
print(name)
name=' Joss '
print(name)

Common data types

  • int Integer types

integer, Can represent a positive number , negative ,0

# Integer types , Can represent a positive number , negative ,0
n1=90
n2=-76
n3=0
print(n1,type(n1))
print(n2,type(n2))
print(n3,type(n3))
# Positive numbers can be expressed as binary numbers , Decimal system , octal , Hexadecimal , Default to decimal
print(' Decimal system ',118)
print(' Binary system ',0b10101111)# Binary in 0b start
print(' octal ',0o176)# Octal to 0o start
print(' Hexadecimal ',0x1EAF)# Hex to 0x start 
  • float Floating point type

a=3.14159
print(a,type(a))
n1=1.1
n2=2.2
n3=2.1
print(n1+n2)# The computer stores floating point numbers imprecisely resolvent : The import module decimal
print(n1+n3)
from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))
  • bool Boolean type

Used to indicate true or false values

#bool
f1=True
f2=False
print(f1,type(f1))
print(f2,type(f2))
# Boolean values can be converted into integers to calculate
print(f1+1) #2 1+1=2 True Express 1
print(f2+1) #1 0+1=2 False Express 0
  • str String type

Strings are also called immutable character sequences

You can use single quotes , Double quotes , Three quotation marks to define

The string defined by single quotation marks and double quotation marks must be on one line

A string defined by three quotation marks can be distributed on multiple consecutive lines

str1=' Life is too short , I use Python'
print(str1,type(str1))
str2=" Life is too short , I use Python"
print(str2,type(str2))
str3=""" Life is too short ,
I use Python"""
print(str3,type(str3))
str4=''' Life is too short ,
I use Python'''
print(str4,type(str4))

Data type conversion

name = ' Zhang San '
age = 20
print(type(name), type(20))  # explain name And age The data types of are different
# print(' My name is '+name+' This year, '+age+' year ') # When will str The type and int Type , Exposure error Solution : Type conversion
print(' My name is '+name+' This year, '+str(age)+' year ')   # take int Type through str() The function becomes str type
print("----------str() Change other types to str type ---------")
a = 10
b = 198.8
c = False
print(type(a), type(b), type(c))
print(str(a), str(b), str(False))
print(type(str(a)), type(str(b)), type(str(False)))
print("----------int() Change other types to int type ---------")
s1 = "128"
f1 = 98.7
s2 = "76.77"
ff = True
s3 = 'hello'
print(type(s1), type(f1), type(s2), type(ff), type(s3))
print(int(s1), type(int(s1)))  # take str Turn into int type , The string is a numeric string
print(int(f1), type(int(f1)))  # float Turn into int type , Truncate the integral part , Omit the decimal part
# print(int(s2), type(int(s2))) # take str Turn into int type , Exposure error , Because the string is a decimal string
print(int(ff), type(int(ff)))   # take str To int type , The string must be a number string ( Integers ), Non numeric strings are not allowed to be converted
print("----------float() Change other types to float type ---------")
s1 = "128.98"
s2 = "76"
ff = True
s3 = 'hello'
i = 98
print(type(s1), type(s2), type(ff), type(s3))
print(float(s1), type(float(s1)))
print(float(s2), type(float(s2)))
print(float(ff), type(float(ff)))
# print(float(s3), type(float(s3))) # If the data in the string is a non numeric string , Conversion is not allowed
print(float(i), type(float(i)))

Python The comments in

  • An explanatory text that explains the function of the code in the code

  • Note types

    - Single-line comments : With # start , Until the end of the line feed
    - Multiline comment : There are no separate multiline comment tags , Turn the code between a pair of three quotation marks into a multiline comment
    - Comments on Chinese coding statement : Add a Chinese statement note at the beginning of the document , Used to specify the encoding format of the source file 
    # Output function ( Single-line comments )
    print('hello')
    ''' Hey , I'm a multi-line comment '''
# coding:gbk

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved