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

[Python] Python Basics

編輯:Python

perform

  • Windows
 Mode one
D:\Python\python.exe D:\Python17\DAY01\Helloworld.py
Mode two
D:\Python\python.exe
  • Linux
 Mode one
[[email protected] ~]# /usr/bin/python Helloworld.py
Mode two
[[email protected] ~]# python
Mode three
[[email protected] ~]# vim Helloworld.py
#!/usr/bin/python // Add header information
# -*- coding:utf8 -*- // Interpreter code
print('Hello Python!')
[[email protected] ~]# chmod +x Helloworld.py // Add executable rights
[[email protected] ~]# ./Helloworld.py

code

  • Character encoding In essence, computers only know binary 0 and 1, It can be said that the actual physical representation of any data in the computer is 0 and 1, All information is ultimately represented as a binary string , Every binary bit (bit) Yes 0 and 1 Two kinds of state .
  • ASCII ASCII(American Standard Code for Information Interchange, American standard information exchange code ) The coding rules are customized by the United States , It's stipulated that 128(2**8) Character encoding , Such as spaces SPACE yes 32( Binary system 00100000), Capital letters A yes 65( Binary system 01000001). this 128 Symbols ( Include 32 A control symbol that can't be printed ), It only takes up the end of one byte 7 position , The first one is uniformly defined as 0.
  • GB2312、GBK Although computers were invented by Americans , But people all over the world are using computers . Now there's another problem : How to make Chinese understood by computer ? This is the trouble , Unlike Latin, Chinese is made up of fixed alphabets .ASCII Code obviously can't solve this problem , In order to solve this problem, the State Administration of standards of China 1980 Released in 《 Chinese character coded character set for information interchange 》 Put forward GB2312 code , Used to solve the problem of Chinese character processing .GB2312 Appearance , Basically meet the needs of computer processing of Chinese characters , But for names 、 Rare words in ancient Chinese ,GB2312 Can't handle , That led to GBK code .
  • Unicode With the popularity of computers , obviously ASCII Code cannot represent all kinds of words and symbols in the world , So was born Unicode code . Unicode( Unified code 、 unicode 、 Single code ) It's a character encoding used on a computer .Unicode It is to solve the limitation of traditional character coding scheme , It sets a uniform and unique binary encoding for each character in each language , All characters and symbols shall be specified by 16 To said (2 Bytes ).
  • UTF-8 because Unicode Included more characters , It can be imagined that its analytical efficiency is higher than ASCII The speed of the code should be greatly reduced , as a result of Unicode By adding a high byte pair ISO Latin-1 Character set extension , When these high byte bits are 0 when , The low byte is ISO Latin-1 character . Yes, it can be used ASCII The characters are represented by Unicode Not efficient , because Unicode Than ASCII Take up twice as much space , And yes ASCII For high byte 0 It's no use to him . To solve this problem , And that's what happened UTF-8 code .UTF-8 The length of coding can be automatically selected according to different symbols , For example, English letters can only be used 1 A byte is enough .
  • Code summary To process English characters , There is ASCII code . In order to process Chinese characters , There is GB2312 code . In order to process national characters , There is Unicode code . In order to improve the Unicode Storage and transmission performance , There is UTF-8 code .

notes

  • Single-line comments

Often used as a single line notation , Used in code # Any data on its right will be ignored , As a comment .

# Here's a single line of notes
# print('Hello Python!')

Multiline comment Multiline comments are in triple quotes """ """ Contains .

# Here's a multiline comment
"""
print('Hello Java!')
print('Hello PHP!')
"""

quotes

  • Single quotation marks
a = ' Who said my eyes are only you , Let me forget you !'
print(a)
// Single quote string 
  • Double quotes
a = " Who said my eyes are only you , Let me forget you !"
print(a)
// Double quote string 
  • Three quotes
a = ''' Who said my eyes are only you , Let me forget you !'''
print(a)
// Three single quote strings , Support for line breaks
a = """ Who said my eyes are only you , Let me forget you !"""
print(a)
// Three double quotes string , Support for line breaks 

Variable

  • Definition
a = ' Who said my eyes are only you , Let me forget you !'
print(a)
// A variable called :a, Variable a The value of is :' Who said my eyes are only you , Let me forget you !'
  • Naming rules Letter , Numbers , Underline composition Cannot start with a number Cannot use keyword Variable names are known by name
  • assignment
name = ' Lin bin '
user = name
// Variable name,user Point to the same address in memory
name = ' Lin bin '
user = ' Lin bin '
// Variable name,user Point to the same address in memory ,Python Memory optimization mechanism of
name = ' Senior brother '
print(name) // Print elder martial brother
user = name
print(user) // Print elder martial brother
name = ' Second senior brother '
print(name) // Print Second Senior brother
print(user) // Print elder martial brother 

Input and output

  • Input
name = input(' Please enter a user name :')
print(name)
import getpass
password = getpass.getpass(' Please enter your password :')
print(password)
  • Output
a = 'Hello Python!'
print(a)

Conditional statements

  • Single branch structure
if 1 == 1:
print(' The conditions are right ')
if 1 == 1:
print(' The conditions are right ')
else:
print(' Condition error ')
  • Multi branch structure
name = input(' Please enter a user name :')
if name == 'linbin':
print(' Super administrator ')
elif name == 'LinBin':
print(' Administrators ')
elif name == 'LB':
print(' Ordinary users ')
else:
print(' Anonymous users ')
  • Nested structure
name = input(' Please enter a user name :')
if name == 'linbin':
password = input(' Please input a password :')
if password == 'axbc1kof':
print(' Login successful !')
else:
print(' Login failed !')
else:
print(' Who are you? ?')

Loop statement

  • while The given judgment condition is True When the loop body , Otherwise, exit the circulatory body .

continue: Jump out of this cycle , So let's go to the next loop break: Out of the loop

i = 0
while i < 11:
print(i)
i += 1
  • for Repeat the statement .

continue: Jump out of this cycle , So let's go to the next loop break: Out of the loop

a = ['a','b','c','d']
for item in a:
print(item)

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