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))
命令行方式实现: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))
相关文章推荐
- Day1 Python基础学习——概述、基本数据类型、流程控制
- Python学习笔记一:数据类型与基本操作
- Python学习笔记(二):基本数据类型及操作(逻辑、字符串、浮点、复数)
- Python基础,基本类型(整型,浮点数等)数据结构(List,dic(Map),Set,Tuple),控制语句(if,for,while,continue or break):来自学习资料
- Python学习 第4天 基本语法-变量、数据类型、运算符、流程控制
- python学习 变量的操作 与 基本数据类型
- python语法31[基本数据类型和流程控制]
- 黑马程序员-操作基本数据类型的流对象学习日记
- Python快速学习02:基本数据类型 & 序列
- Python3 学习第一弹:基本数据类型
- python学习笔记之-基本数据类型
- 【python学习笔记二】基本数据类型
- python学习笔记-(2)Python的基本数据类型
- python学习笔记6-基本对象和流程语句整理
- Swift 学习笔记一 基本数据类型和简单语句语法
- Python源码学习笔记(1 基本数据类型)
- Python快速学习02:基本数据类型 & 序列
- 黑马程序员-学习日志-操作基本数据类型的数据流
- Java学习笔记001——学前准备、数据类型、运算符、流程控制语句
- Python学习笔记(二):语句、数据类型(快速入门篇)