o(* ̄︶ ̄*)o

  博客园  :: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理

python 入门预览

#coding=utf-8
#   1-1 
#上面声明代码断, #表示屏蔽当前行,
#使用utf-8处理中文,避免无法编译乱码什么的


#1-2
#python 输出方式
str1 = "hello, world!!"
print "str: " + str1
print 'hello, world!!'
print("hello world")   
print "hello, world!!"


#1-3
#数字和表达式
print 1.0/2
print 1/2         # 在文件开头不导入 from __future__ import division
                  # (future 前后两个下划线) 结果为0,
				  # 导入后结果为0.5
				  # 在Linux 中 使用命令-Qnew 同样能达到这样效果

print 1 //2       # //实现整除操作符,无余数,小数点; 为0
print 1.0 // 2    #  结果:0.0
print 1%2         # 结果 1, 取余数

#次方计算
print 2 **3       # result: 8
print pow(2, 3)   # 同上功能
print -2 ** 4     # result: -8
print  (-2) ** 4

#1-4
#长整形
print 1000000L    # L表示长整型,不建议小写,避免与1混淆

#十六进制,八进制, 二进制
print 0xAF        # 0x 十六进制
print hex(10)     #十进制转换十六进制

print 010         # 0开头 八进制
print oct(10)     # 十进制转换八进制 

print bin(10)     # 十进制转换二进制
print 0b0101      # 二进制 以0b开头


#1-5
#变量
#无需声明变量类型
x = 3
print x *6

#1-6
#语句
print 2*2
print (2*2)  #3.0版本print 是函数 必须这样写,

#1-7
#输入
if 1 == 2 :
	number = input("which number by to you like? input:")  #括号内可以为空, 只能输入数字
	print number * 10

if 1 == 3:
	name = raw_input("what's your name? tell me :")        #
	print "hello " + name 


#1-8
#函数
print pow(2, 3)  	# 阶乘
print abs(-10)   	# 绝对值  |-10| = 10
print round(1.0/2)  # 四舍五入函数,值是浮点数
print round(5.1)
print round(5.9)
print round(10)

1-9
#导入模块: 为了引用其他文件写好的接口进行调用
import math 	        # 加载数学库
print math.floor(23.9)  # 向下取整
print math.ceil(23.9)   # 向上取整  

from math import sqrt   # 只导入一个函数 ; 不建议使用,避免相同函数名冲突
print sqrt(9)

import cmath            # 复数计算
print(cmath.sqrt(-1))   # j表示 复数 
print (1 + 3j) * (9 + 4j)

#1-10
#字符串
print "Let's go!"        #双引号
print 'Let\'s go!!'      #单引号,内部必须使用转义字符
# 多行
print """ wo hao hao
      xiang , hao hao xiang 
      he ni zai yi qi !!"""
#	  
print ''' wo hao hao
      xiang , hao hao xiang 
      he ni zai yi qwerr '''  
print "hello " " wrold!!"    #字符串空格相互连接

x = "a +"
y = "b"
print x + y	 


# 原始字符串,不进行转义, 结尾必须不能是\'
#特色:以r开头
print r'D:\\my think\avi'
#Unicode 字符串,将 ascii码(8位)转换Unicode码(16位)
#特色:以u开头
print u'Hero world'

print 10000L
print repr(10000L)     # repr 将参数放入到字符串里
print str(1000000L)    # str 将参数转换为合理的字符串

#right code
num = 100
print "get money: " +repr(num)
#Error Code 
# num = 100
# print "get money: " + num

	  

  

posted on 2015-03-05 16:00  熊本熊の熊  阅读(218)  评论(0)    收藏  举报