安装
安装python
访问官网,选择downloads选项,下载exe文件,安装
安装pycharm
访问pycharm网站,下载pycharm
参考这篇文章来实现安装的具体步骤
检测是否安装成功
如果安装成功,命令输入python,显示如下
1.python基础语法
1.1变量和字符串
- 变量
a = 4;
b = 5;
t = a;
print(t) ==> 4
- 字符串加法
a = 'my name';
b = ' is';
c = ' shizhihua';
print(a+b+c) ==> my name is shizhihua
- 字符串乘法
a = 'haha';
print(a*3) ==> hahahahahaha
- 字符串的切片和索引
通过string[x],获取字符串的一部分信息
a = 'my name is shizhihua';
print(a[0]) ==> m #取字符串第一个元素
print(a[0:5]) ==> my na #取字符串第一个到第五个元素
print(a[-1]) ==> a #取字符串最后一个元素
1.2字符串方法
-
split() 讲一个字符串分割为一个列表
a = 'www.baidu.com';
print(a.split(".")) ==> ['www','baidu','com']
-
replace() 类似文本中的“查找和替换”功能
a = 'There is apples';
b.replace("is","are")
print(b) ==> There are apples
-
strip() 返回除去两侧(不包括内部)空格的字符串,也可以指定需要去除的字符,将它们列为参数中即可
a = ' python is coool ';
print(a.strip()) ==> python is coool
- format() 字符串格式化符
a = '{} is my love'.format('Python');
print(a) ==> Python is my love
1.3函数与控制语句
-
函数
def 函数名(参数1,参数2…)
return ‘结果’
def function1(a,b)
return '1/2*a*b'
function1(6,8)
-
判断语句
冒号和缩进不要忘记
if condition:
do
else:
do
多重条件的格式
if condition:
do
elif condition:
do
else:
do
例子
def count_login():
password = input('password')
if password == '12345':
print('输入成功!')
else:
print('错误,再输入')
count_login()
count_login()
- 循环语句
for循环
for item in iterable
do
while循环
while condition:
do
计算1~100的和
i = 0
sum = 0
while i<100
i = i+1
sum = sum + 1
print(sum) ==> 5050
1.4python数据结构
1.4.1列表
列表每一个元素都是可变的;都是有序的;可容纳所有的对象
list = ['peter','lilei','wangwu','xiaoming']
print(list[0]) ==>peter
print(list[2:]) ==> ['wangwu','xiaoming']
1.4.2字典
字典数据结构与现实中的字典类似,以键值对的形式表现出来。
user_info = {
'name':'xiaoming',
'age':'23',
'sex':'man'
}
1.4.3元组和集合
元祖类似于列表,但是元祖的元素不能修改,只能查看
tuple = (1,2,3)
集合的概念类似数学中的集合。每个集合中的元素是无序的,不可以有重复的对象,因此可以通过集合把重复的数据去除。集合使用大括号构建的。
list = ['xiaoming','zhangyun','xiaoming']
set = set(list)
print(set) ==> {'zhangyun','xiaoming'}
1.5Python文件操作
1.5.1打开文件
open(name[,mode[,buffering]])
open()函数使用文件名作为唯一的强制参数,然后返回一个文件对象。模式(mode)和缓冲(buffering)是可选参数
f = open('c:/users/administrator/Desktop/file.txt')
值 |描述
r | 读模式
w | 写模式
a | 追加模式
b | 二进制模式(可添加到其他模式中使用)
+ | 读/写模式(可添加到其他模式中使用)
1.5.2读写文件
有了名为f的类文件对象,那么就可以通过f.write()方法和f.read()方法写入和读取数据了
f = open('c:/users/administrator/Desktop/file.txt','w+')
f.write('hello world')
f = open('c:/users/administrator/Desktop/file.txt','r')
content = f.read()
print(content) ==> hello world
1.5.3 关闭文件
当完成读写工作后,应牢记使用close()方法关闭文件。这样可以保证Python进行缓存的清理(处于效率考虑把数据临时存储在内存中)和文件的安全性
f = open('c:/users/administrator/Desktop/file.txt','r')
content = f.read()
print(content) ==> hello world
f.close()
1.6Python面向对象
1.6.1定义类
通过class定义一个自行车的类,类中的变量compose称为类的变量,专业术语为类的属性。
class Bike:
compose = ['frame','wheel','pedal']
1.6.2实例属性
compose属性属于所有的该款自行车,而other属性只属于my_bike这个类的实例
class Bike:
compose = ['frame','wheel','pedal']
my_bike = Bike()
my_bike.other = 'basket'
print(my_bike.other)
1.6.3实例方法
方法是对实例进行使用的
class Bike:
compose = ['frame','wheel','pedal']
def use(self)
print('you are riding')
my_bike = Bike()
my_bike.use()
1.6.4类的继承
class Bike:
compose = ['frame','wheel','pedal']
def _init_(self):
self.other = 'basket'
def use(self,time):
print('you ride {}m'.format(time*100))
class Share_bike(Bike):
def cost(self,hour):
print('you spent {}'.format(hour*2))
bike = Share_bike()
print(bike.other) ==> basket
bike.cost(2) ==> you spent 4