Home  >  Article  >  Backend Development  >  Detailed explanation of Python syntax basics

Detailed explanation of Python syntax basics

小云云
小云云Original
2018-03-30 16:31:272156browse

This article mainly shares with you a detailed explanation of the basics of Python syntax, hoping to help you.

Python comments

'''多行注释'''
井号单行注释

python2 When Chinese characters exist in the file, they must be processed (in two ways)

  1. Add to the file header #coding=utf-8

  2. Add #-*- coding:utf-8 -*- to the file header (python recommended )

python input, output

  1. python3 input: The value entered in input is the value of the variable on the left

    heigh=input("请输入值:")
    print("heigh:%d"%heigh)

    Result It’s

    #heigh:3

  2. Python2 input: The value entered in input is executed as a string of codes, so raw_input should be used. Input

    #coding=utf-8
    a=raw_input("输入值:")
    print a
    b=input("请输入:")
    print

and the result is

    输入值:12
    12
    请输入:1+5
    6

3. Python outputs the values ​​​​of multiple variables at the same time
Name=”shushu”
Age=20
       addr=”Dalian, Sichuan”
Print("Name: %s, Age: %d, Address: %s" %(name,age,addr))
The result is:
Name: shushu, age: 20, address: Dalian, Sichuan

Logical operator

and and
or or
nonnot

judgment statement

    if age>18:
        print("成年了")
    else:
        print("未成年")

    if age>18:
        print("成年了")
    elif age>10:
        print("少年")
    else:
        print("儿童")

Loop statement

while loop

    i=1;
    while i<10:
        print(i)
        i+=1

for loop

In Python, the for loop can variable any sequence of items, such as a List or a string, etc.

     for 临时变量 in 列表或者字符串等:
        循环满足条件时执行的代码
    else:
        循环不满足条件时执行的代码

For example:

      name = &#39;dongGe&#39;
      for x in name:
            print(x)

will output

    d
    o
    n
    g
    G
    e

Related recommendations:

python syntax marshmallow Description

Python syntax exercise--while loop

Python syntax quick start guide

The above is the detailed content of Detailed explanation of Python syntax basics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn