python 入门

python 入门

#!/usr/bin/python
#coding=utf-8
if True:
    print "True"
else:
    print "False"
    print "hello world"

#total = item_one + item_two + item_three
days = ['Monday', 'Tuesday', 'wednesday']
print days
print 'hwllo world'

#if expression:
#    print "1"
#elif expression1:
#    print "2"
#else expression3:
#    print "3"

counter = 100
miles = 100.0
name = "jeffasd"
print counter
print miles;
print name
a = b = c = 2
a, b, c = 1, 2, "jeffasd"

var1 = 1;
del var1;

str = "hello world"


print str;
print str[0]
print str[2:4]
print str[2:]
print str * 2
print str + "hwllow"


#Python列表
list = ['runoob', 789, 2.23, 'josh', 30.2]
tinylist = [232, 'john']
print list
print list[0]
print list[1:3]
print list[2:]
print tinylist *2
print list + tinylist

#Python元组
#元组用"()"标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。
tuple = ('runoob', 'sfdsf', 89, 23.3)
tupleTuple = (1232, 'josh')
print tuple;
print tuple *2
print tuple + tupleTuple
#元组不允许赋值
#tuple[2] = 23

#python字典
dict = {}
dict['one'] = "THis is one"
dict[2] = "This is two"
tinyDict = {'name':"value", "key":"value2"}
print dict
print dict['one']
print dict[2]
print tinyDict
print tinyDict.keys()
print tinyDict.values()

inta = 1
intb = 2
if (inta == intb):
    print "相等"
if inta != intb :
    print "不相等"

if inta and intb :
    print "and"
if inta or intb :
    print "or"

if not inta and intb :
    print "not and"

if not False :
    print "not and -"

intArray = [1, 2, 3, 4, 5]
if intb in intArray :
    print "在数组内"
if inta is intb :
    print "相等"
if inta is not intb :
    print "不相等"

flag = False
name = 'luren'
if name == "python" :
    flag = True
    print "welcome"
else:
    print name

count = 0
while count < 9 :
    count += 1
    print count

for letter in 'python' :
    print "当前字母是:", letter

fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)) :
    print '当前水果:', fruits[index]

#循环使用 else 语句
#在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
for num in range(10,20):  # 迭代 10 到 20 之间的数字
    for i in range(2,num): # 根据因子迭代
        if num%i == 0:      # 确定第一个因子
            j=num/i          # 计算第二个因子
            print '%d 等于 %d * %d' % (num,i,j)
            break            # 跳出当前循环
    else:                  # 循环的 else 部分
        print num, '是一个质数'

#判断素数比较牛逼的算法
i = 2
while(i < 100):
    j = 2
    while(j <= (i/j)):
        if not(i%j): break
        j = j + 1
    if (j > i/j) : print i, " 是素数"
    i = i + 1

print "Good bye!"

#Python pass是空语句,是为了保持程序结构的完整性。
#pass 不做任何事情,一般用做占位语句。
#Python 语言 pass 语句语法格式如下:
for letter in 'python':
    if letter == 'b':
        pass
        print '这是pass块'
    print '当前字母:',letter

import time
ticks = time.time()
print ticks

localTime = time.localtime(time.time())
print localTime

localtime = time.asctime(time.localtime(time.time()))
print localtime

def printime(str):
    print str
    return

printime("hwllo");

#不定长参数
def printInfo(arg1, *vartuple):
    print arg1
    for var in vartuple:
        print var
    return

printInfo(10)
printInfo(60, 10, 40)

#匿名函数
sum = lambda arg1, agr2: arg1 + agr2
print sum(10, 20)
print sum(20, 30.4)

#return 语句return语句[表达式]退出函数,选择性地向调用方返回一个表达式。不带参数值的return语句返回None。
def sum(agr1, agr2):
    total = agr1 + agr2
    print total
    return total
total = sum(3, 5)
print total

#全局变量和局部变量
total = 0; # 这是一个全局变量
# 可写函数说明
def sum( arg1, arg2 ):
    #返回2个参数的和."
    total = arg1 + arg2; # total在这里是局部变量.
    print "函数内是局部变量 : ", total
    return total;

#调用sum函数
sum( 10, 20 );
print "函数外是全局变量 : ", total

import math
content = dir(math)
print content





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值