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

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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值