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

Python learning (I) basic syntax and input / output functions

編輯:Python

Basic grammar

1. notes :

Annotations are not translated into machine code in the interpreter , Its existence does not affect the function of the program

  1. Single-line comments : Add... Before the notes #
  2. Multiline comment : ''' Multiline comment content ''' perhaps """ Multiline comment content """

Annotate the code with , Improve the readability of the code .

2. Lines and indents

  1. python One line of code
  2. Indent : The space before a piece of code is called indentation , Number of indents default 4 A space

3. Identifiers and keywords

  1. identifier ---- Naming requirements

     python Identifier requirements , There are letters 、 Numbers 、 Underline composition , And numbers don't start
    
  2. keyword ---- python Some identifiers with special functions or special meanings in , It can't be used for other purposes !

Reserved words :

['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']

# View reserved words 
from keyword import kwlist
print(kwlist)

4. Common data and data types

  1. Digital data : Data used to represent the size of a numeric value

    The way in which numerical data is represented in a program , It's the same way in mathematics that it's a number , And support scientific counting
    The type corresponding to the number : Digital data is divided into integer types (int - Integers ) And floating point (float - decimal ) Two kinds of

  2. Text data :

    It is used to represent the data corresponding to text information ,
    Representation : The text data must be enclosed in quotation marks
    type : character string (str )

  3. Boolean data :

    Only True and False Two values True To affirm 、 really ;False Said the false 、 no (True and False Is the key word )
    Representation : direct writing True、False.
    type : Boolean (bool

  4. Null value :

    Only None (None Is the key word )
    type :NoneType

5. Type operation

  1. Get data type : type( data ) Gets the type of the specified data

  2. Type conversion : Type name ( data ) The specified data is converted to the specified type

  3. int and float They can switch to each other ,int turn float Add ' .0', float turn int Go directly to the integer part of the decimal

  4. Strings and numbers and conversions
    String after removing quotation marks A string that is itself an integer can be converted to int
    String after removing quotation marks A string that is itself a numeral can be converted to float

  5. Other data are transferred to str
    All types All the data can be converted into str, When converting, directly put quotation marks outside the original data

  6. bool Convert numbers
    True -> 1/1.0
    False -> 0/0.0

  7. Other data are transferred to bool
    All types All the data can be converted into bool, be-all Both zero and null values are False , Everything else is True

Input and output functions

1. Output function

def print(self, *args, sep=' ', end='\n', file=None): # \n Namely 
pass
# print : Is to display the data in the program on the console ( Print ) come out , All the contents displayed in the console , It must be used in the program print Printed content 
  1. Basic usage
    1. One print Print a data : print( data )
    2. One print Print multiple data : print( data 1, data 2,.......)
  2. Advanced usage
    1. end Usage of : Program execution print During operation , Will print the data first , The data will be printed after printing end Value (end The default value of is line feed )
    2. sep Usage of : Program execution print When printing multiple operations , Will insert... Between values sep Value , Control when printing multiple data at the same time , A separator between data and data (sep The default value of is space )

2. Input function

def input(*args, **kwargs):
pass
# The program gets data from the console 

Variable = int(input(" Enter the prompt message ")) ------ Enter the prompt message , And save the input to the variable


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