Detailed explanation of Python syntax basics

小云云
Release: 2018-03-30 16:31:27
Original
2095 people have browsed it

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

Python comments

'''多行注释'''
井号单行注释
Copy after login

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)
    Copy after login

    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
    Copy after login

and the result is

    输入值:12
    12
    请输入:1+5
    6
Copy after login

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("未成年")
Copy after login

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

Loop statement

while loop

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

for loop

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

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

For example:

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

will output

    d
    o
    n
    g
    G
    e
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!