0520 python

配置python环境变量
我的电脑-》右键-》属性-》高级系统设置-》环境变量-》
(1)用户变量-》新建 Path=C:Python27
(2)系统变量-》编辑 Path 值后添加 ;C:Python27

用户变量只对单个用户有效
系统变量对所有用户有效

windows中DOS窗口执行命令时,先在当前目录下寻找可执行exe文件,然后在环境变量Path中找。环境变量配置后,需要重启CMD窗口使配置生效

 

开始我们的 hello world 之旅
cmd下执行
C:Users***>python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello World!"
Hello World!

 

可以打印字符串(中文和英文,需要引号)、数字(数字不需要引号)
>>> print 2
2
>>> print "中文"
中文
>>> print 'abc'
abc
>>> print 'abc 1'
abc 1

 

变量:可以理解为一个鞋盒,值由放入什么内容决定
变量可以存储的内容:
(1)数
(2)字符串
>>> a=1
>>> a
1
>>> b=2
>>> a=2
>>> a
2
>>> a=3
>>> a
3
>>> a="abc 中文"
>>> a
'abc xd6xd0xcexc4'
>>> print a
abc 中文

交互环境中,默认的打印方式(即不使用print)内部是使用repr,如果使用print打印的话 则是使用str。

str()将对象转换为人们易读的字符串,repr()将对象转换为python内部表示的 字符串形式。

>>> num=1/3.0
>>> num
0.3333333333333333
>>> repr(num)
'0.3333333333333333'
>>> str(num)
'0.333333333333'
>>> print num
0.333333333333

 

>>> str1="hello"
>>> str1
'hello'
>>> repr(str1)
"'hello'"
>>> str(str1)
'hello'
>>> print str1
hello

 

退出python交互模式有两种:
>>> ^Z 【Ctrl+Z,推荐】
>>> exit()
>>>【Ctrl+C 键盘中断】
KeyboardInterrupt

 

查看python版本
C:Users***>python -V
Python 2.7.11

 

数:整数、长整数、浮点数(包括科学计数法)、复数
>>> a=1
>>> a
1
>>> b=1234567890
>>> b
1234567890
>>> c=1.123456789
>>> c
1.123456789
>>> d=5+4j
>>> d
(5+4j)
>>> e=1.2E3
>>> e
1200.0
>>> e=1.3e-3
>>> e
0.0013

 

标识符命名规则
(1)首字母必须是字母或下划线
(2)其他部分可以是字母、数字和下划线
(3)区分大小写,命名要见名知意
>>> userName="Lucy"
>>> userName
'Lucy'
>>> print userName
Lucy
>>> userId=123
>>> userId
123
>>> print userId
123

 

type() 查看变量类型
>>> a=1
>>> type(a)
<type 'int'>
>>> b="abc"
>>> type(b)
<type 'str'>

 

字符串
单引号,双引号,使用完全相同
三引号,可以保持字符串的格式
>>> a="a"
>>> a
'a'
>>> a='a'
>>> a
'a'
>>> a="""1
... 2
... 3
... 4"""
>>> a
'1 2 3 4'
>>> print a
1
2
3
4
>>> a='''
... 1
... 2
... 3
... 4
... '''
>>> a
' 1 2 3 4 '
>>> print a

1
2
3
4

>>>

 

换行符   制表符
>>> a="1 2 3"
>>> print a
1
2
3
>>> print "a  b"
a
b

转义字符
>>> print "a \b"
a 
>>> print "a \nb"
a b

 

折行的处理
>>> print "This is the first sentence.
... This is the second sentence."
This is the first sentence.This is the second sentence.

作业:定义任意两个数字,做+ - * / % **运算

交互环境下运行
>>> a=1
>>> a=5
>>> b=2
>>> print a+b
7
>>> print a-b
3
>>> print a*b
10
>>> print a/b
2
>>> print a%b
1
>>> print a**b
25

test.py 文件
# -*- coding: utf-8 -*-
a=int(raw_input("Enter a: "))
b=int(raw_input("Enter b: "))

print u"加 a+b= ", a+b
print u"减 a-b= ", a-b
print u"乘 a*b= ", a*b
print u"除 a/b= ", a/b
print u"取余 a%b= ", a%b
print u"幂 a**b= ", a**b

DOS窗口命令
切换磁盘目录
C:Users***>d:
切换目录
D:>cd python
查看目录内容
D:python>dir
驱动器 D 中的卷没有标签。
卷的序列号是 0D08-052F

D:python 的目录

2016/05/25  10:14    <DIR>          .
2016/05/25  10:14    <DIR>          ..
2016/05/25  11:05             2,521 0520.py
2016/05/25  11:11               242 test.py
               2 个文件          2,763 字节
               2 个目录 42,493,263,872 可用字节

D:python>python test.py
Enter a: 7
Enter b: 3
加 a+b=  10
减 a-b=  4
乘 a*b=  21
除 a/b=  2
取余 a%b=  1
幂 a**b=  343

原文地址:https://www.cnblogs.com/blueskylcc/p/5527334.html