Python入门系列教程(一)基础

序言

基础知识

1.变量及类型

2.换行

3.输入

password = raw_input("请输入密码:")
print '您刚刚输入的密码是:', password

4.格式化输出

a="hello"
print('输出%s'%a)

5.ctrl+/注释或者是取消注释

6.sleep

import  time
time.sleep(2)

7.类型转换

int(x [,base ]) 将x转换为一个整数

long(x [,base ]) 将x转换为一个长整数

float(x ) 将x转换到一个浮点数

complex(real [,imag ]) 创建一个复数

str(x ) 将对象 x 转换为字符串

repr(x ) 将对象 x 转换为表达式字符串

eval(str ) 用来计算在字符串中的有效Python表达式,并返回一个对象

tuple(s ) 将序列 s 转换为一个元组

list(s ) 将序列 s 转换为一个列表

chr(x ) 将一个整数转换为一个字符

unichr(x ) 将一个整数转换为Unicode字符

ord(x ) 将一个字符转换为它的整数值

hex(x ) 将一个整数转换为一个十六进制字符串

oct(x ) 将一个整数转换为一个八进制字符串

8.if语句

score=100
if score>90:
    print "优秀"
print "不够优秀"

9.if-else

score=100
if score>90:
    print "优秀"
else:
    print "不够优秀"

10.not

if not(score>90 and score<=100):
    print "不够优秀"
else:
    print "优秀"

11.elif

if score>90 and score<=100:
    print "优秀"
elif score>60 and score<90:
    print "不够优秀"
elif score<60:
    print "不及格"

12.if套嵌

if score>90 and score<=100:
    if score==100:
        print "完美"
    else:
        print "优秀"
else:
    print "不够优秀"

13.随机数

import  random
print  random.randint(2,7)

14.while循环

i=1
while i<10:
    print ("当前是第%d次循环"%(i))
    i+=1

15.for循环

name="cnki"
for temp in name:
    print temp

16.break

name="cnki"
for temp in name:
    if  temp=='k':
        break
    print temp

17.continue

name="cnki"
for temp in name:
    if  temp=='k':
        continue #不执行后面的代码,直接进入下一次循环
    print temp

18.支持中文

#encoding=utf-8
原文地址:https://www.cnblogs.com/cnki/p/5637934.html