Python3基础语句

import语句

作用:引入模块

# 1
import math
math.pow(3,2)

# 2
from math import pow
pow(3,2)

# 3
from math import pow as pingfang
pingfang(3,2)

# 4
from math import *
pow(3,2)

条件语句

if

if a == 8:
	print(a)

if...elif...else

#! /usr/bin/env python
# -*-coding:utf-8-*-

if number == 10:
	print(number)
elif number > 10:
	print("number > 10")
else
	print("number < 10")

三元操作符

A = Y if X else Z
  • 如果X为真,那么就执行A=Y
  • 如果X为假,就执行A=Z

for循环语句

for i in hello:
	print(i)

range(start,stop[,step])

for i in range(10):
	print("loop:", i )

for...else

from math import sqrt
for n in range(99, 1, -1):
	root = sqrt(n)
	if root == int(root)
		print(n)
		break
else:
	print("Nothing.")

while循环语句

count = 0
while count < 10:
	print("你是风儿我是沙,缠缠绵绵到天涯...",count)
	count +=1

break和continue

  • break: 中断循环,跳出循环体。
  • continue:中断本次循环,继续下一次循环

while...else

count = 0
while count < 5:
	print(count, "is less than 5")
	count += 1
else:
	print(count, "is not less than 5")

转载于:https://my.oschina.net/zerobit/blog/3068214

你可能感兴趣的:(Python3基础语句)