Python入门(二)数据处理:数值和字符串

了解python文件结构,
保存成one.py
另外pyc, pyo文件
pyc编译执行的文件
pyo编译优化后的文件

掌握python变量和常量
推荐第一门语言为python
变量:c语言是强类型

变量的命名
有字母、数字
数字不能开头
first= 100
print(first)
id(first)

变量的类型取决于数据
name = 'milo'
print(name)
id(name)

写个四则运算器

from __future__ import division
import sys
running = True
while running:
try:
t = int(raw_input())
p = int(raw_input())
except EOFError:
break

print('operator + result\n', t+p)
print('operator - result\n', t-p)
print('operator * result\n', t*p)
print('operator / result\n', t/p)

[code="java"]
[/code]
算数运算符(加减乘除)
5/2 = 2
5.0/2 = 2.5
5//2 = 2 整除

赋值运算符
a = 3
b = 5
c = a+b

关系运算符(结果为布尔值)
x = 5
x >0 and x<10

NameError: name 'true' is not define
变量没有定义

数据类型
数字、字符串、列表、元组和字典、布尔
a = 100
b = 'hello'
c = '100' 字符串
id(a)
id(c)
a == c result: False

查看数据类型
type(a)
<type 'int'>

q = 10000000000000
type(q)
<type 'long'> 长整形

l = 10L (长整形)
type(l)
<type 'long'>

f = 1.1
type(f)
<type 'float'>

say = "let's go"
print(say)
双引号和单引号,没有太大区别
使用转义"\"来处理单双引号问题
"\n" 换行
"\t" 缩进

docstring使用
s = """ tom:"let's go"
>>> s = """ tom:"let's go"
... jerry:"ok"
... """
>>> print s

序列
>>>s2 = 'abcdefg'
>>>print s2[0]
'a'
>>>s2[2]
'c'
>>>s2[-1] 倒数第一个
'g'

字符串小技巧
>>> x = input()
10
>>> y = input()
20
>>> z = x+y
>>> z
30

练习题
对输入的2个数字加减乘除运算

>>> x = input()
10
>>> y = input()
20
>>> z = x+y
>>> z
30
>>> z = x -y
>>> z
-10
>>> z = x *y
>>> z
200
>>> z = x / y
>>> z
0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值