您的位置:首页 > 编程语言 > Python开发

Python学习第2节(基本数据类型操作和执行流程语句)

2014-07-06 22:48 1046 查看
1、编译成pyc。pyc是编译后平台无关的运行代码,编译后即可保护源码。这里注意,是平台无关,但是和python的版本必须统一。

命令行方式实现:python -m py_compile hello.py

py程序处理:

新建hello2pyc.py,

输入以下内容并保存:

import py_compile

py_compile.compile(r'E:/python/1/hello.py')

也可以输入路径:新建py2pyc.py

输入以下内容并保存:

import py_compile

file = input ("Please enter filename:")

py_compile.compile(file)

2、输入、输出语句

s1=input("Input your name:")

print("hello,%s"%s1)

a=2

b="haha"

c=str(a)+b

d="1111"

e=a+int(d)

print("c is %s,e is %i" %(c,e))

 

3、字符串操作

word="abcdefg"

a=word[2]

print("a is:"+a)

b=word[1:3]

print("b is:"+b)

c=word[:2]

print("c is:"+c)

d=word[0:]

print("d is:"+d)

e=word[2:]+word[:2]

print("e is:"+e)

f=word[-1]

print("f is:"+f)

g=word[-4:-2]

print("g is:"+g)

h=word[-2:]

print("h is:"+h)

i=word[:-2]

print("i is:",i)

l=len(word)

print("Length of word is:"+str(l))

 

4、元组操作

word=['a','b','c','d','e','f','g']

a=word[2]

print("a is:"+a)

b=word[1:3]

print("b is:")

print(b)

print("b is:"+str(b))

c=word[:2]

print("c is")

print(c)

d=word[0:]

print("d is")

print(d)

e=word[:2]+word[2:]

print("e is")

print(e)

f=word[-1]

print("f is")

print(f)

g=word[-4:-2]

print("g is")

print(g)

h=word[-2:]

print("h is")

print(h)

i=word[:-2]

print("i is")

print(i)

l=len(word)

print("Length of word is:"+str(l))

print("Add new element")

word.append('h')

print(word)

del word[0]

print(word)

del word[1:3]

print(word)

 

5、字典操作

x={'a':'aaa','b':'bbb','c':12}

print(x['a'])

print(x['c'])

for key in x:

    print("Key is %s and walue is %s"%(key,x[key]))

 

6、if及for的用法

x=int(input("Please enter an integer:"))

if x<0:

    x=0

    print("Negative changed to zero")

elif x==0:

    print("Zero")

else:

    print("More")

#loop

a=['cat','window','defenestrate']

for x in a:

    print(x,len(x))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: