Python基础简单概述

概述:
Python简介:Python是一种解释型(这意味着开发过程中没有了编译这个环节)、面向对象(支持面向对象的风格或代码封装在对象的编程技术)、动态数据类型的交互式(可在命令行中通过Python提示符及直接代码执行程序)高级程序设计语言。


前言


一、print函数

1,可以输出数字和字符串

print(520)
print(11.32)
print('hello world')

注: 字符串需要加上引号,不加引号计算机无法理解可以是单引号也可以是双引号

2, 输出表达式结果

print(1 + 2)
print(2 * 5)

3, 输出到文件当中 ,打开的时候指定路径和打开方式,要使用file=fp 使用'a+'方式打开文件则如果文件不存在创建或者追加。

fp = open('D:/test.txt', 'a+')
print('hello world', file=fp)
fp.close()

4,print不进行换行输出

print('hello', 'world', 'Python')
print(1)

注:Python中的print类似于C++中的cout函数

二、Python中的变量

1,变量的第一种定义方式

无需定义类型直接等于就可以,如下:

变量名 =# Python中的变量
apple = 11
print(apple)

2,变量的第二种定义的方法(类似于初始化列表)

a, b, c = 1, 2, 3
print(a, b, c)

三,Python中的数据类型

1,列表和元组

1,元组tuple和列表list定义方式(列表是中括号,元组是小括号)

a_tuple = (1, 2, 3, 4, 5)
b_tuple = 1, 2, 3, 4, 5

a_list = [6, 7, 8, 9,]

2,利用for循环顺序的输出

for content in a_list:
    print(content)
for content in a_tuple:
    print(content)

3,元组合列表的访问

# 列表和元组的访问
for index in range(len(a_list)):
    print('index = ', index, 'number in list= ', a_list[index])
    print('index = ', index, 'number in tuple = ', a_tuple[index])
# 列表的追加,元组类似
list2 = [1, 3, 4, 5]
list2.append(10)

访问元组合列表的时候类似于C语言中的数组直接利用索引值访问即可。

4,对列表的一些操作
1,一定的位置添加数字,类似于数组

list2.insert(1, 100)

2,移除第一次出现的number

# 移除第一个1,第二个1不收影响
list2.remove(1)
print(list2)
print(list2[0])

3,列表 索引如果是-1,则是最后一位

print(list2[-1])

注:-2是倒数第二位依次类推
4,索引是从某一位到某一位

print(list2[0:3])

5,第一次出现num的索引值获取

num = 10
flag = list2.index(num)
print(flag)

6,统计一个num在列表中出现的次数

list2.count(num)

7,列表排序 默认从小到大 覆盖到原有的 使用reverse = True 从大到小

list2.sort(reverse=True)

8, 多维列表 类似于多维数组

multi_dim_a = [[1,2,4], [2,3,4],[5,6,7,]]
print(multi_dim_a[0][1])

2,字典

概述:字典和前面的列表与元组相比是无序的。
1,字典的定义

dictionary1 = {'apple':[1, 2, 3], 'pear':fun_test(), 'orange':3}
print(dictionary1['apple'])

2,字典元素的删除

del dictionary1['pear']

3,添加一个字典元素

dictionary1['panpan'] = 20

注:字典可以包含字典,列表,函数等。

四、Python中的循环语句和判断语句

1,Python中的循环

1>while循环

condition = 10
while condition < 20:
    print(condition)
    condition = condition + 1

condition = True
while condition:
    print('进入了循环')
    condition = False

2> Python中的for循环

example_list = [1,2,3,4,5,56]
# 迭代
for i in example_list:
    print(i)
    print('iner for')
# 实际是1-9 步长2
for i in range(1, 10, 2):
    print(i)

注:rang顺序产生一个1-9的数

2,Python中的判断

if判断:
# if语句 <= >= ==
x = 3
y = 3
z = 2
# Python支持一下的形式
if x > z > y:
    print('x is granter than y')
if x != y:
    print('x is not equal to y')

# if else
if x < y :
    print('x < y')
else:
    print('x >= y')

# if else else
if x < y:
    print('x < y')
 # else if简写elif
elif x == y:
    print('x = y')
else:
    print('x > y')
print('finish running')

注:对于Python中的break和continue用法和C相同

五,Python中的函数定义

# Python 函数必须使用def定义
def funtion(a, b):
    c = a + b
    print(c)

funtion(1, 3)
# 对于Python中默认参数来说默认值后面也必须是默认值
def slae_car(price, color, brand, is_second_hand, length = 100, hight = 200):
    print('price:', price,
          'color:', color,
          'brand:', brand,
          'is_second_hand', is_second_hand,
          'length:', length,
          'hight:', hight)

slae_car(100, 'red', 'panpan', 'no', length= 200)
# 返回值的函数
def fun():
    a = 100
    print(100)
    return 1

b = fun()
print(b)

全局变量和局部变量

Global_test = 100
test = None
def fun():
    global test
    test = 10
    print(Global_test)
    return Global_test + test
print(fun())
print(test)
# 如果需要在函数内部定义全局变量则需要申明一下,并且在函数前面要定义变量等于None

六,Python中的文件I/O

# Python 的文件IO
text = 'this is my first text. \nthis is next line text. \nthis is third line text'
print(text)
# 文件的写入
my_file = open('F:/file_test.text', 'w')
my_file.write(text)
my_file.close()
# 文件的追加
append_text = '\nthis is append data'
my_file = open('F:/file_test.text', 'a+')
my_file.write(append_text)
my_file.close()
# 文件的读取
read_file = open('F:/file_test.text', 'r')

# content = read_file.read()
firstLine_read = read_file.readline()
print(firstLine_read)
read_file.close()

七,Python中的class类

class 类

class Calculator:
    # 固有属性
    name = 'My name is calculator'
    price = 18
    # 类似于C++函数中的构造函数
    def __init__(self, name, price, width = 90, hight = 100):
        self.name = name
        self.price = price
        self.width = width
        self.hight = hight

    def add(self, x, y):
        result = x + y
        print(result)

    def minus(self, x, y):
        print(x - y)

    def times(self, x, y):
        print(x * y)

    def divide(self, x, y):
        if y != 0:
            print(x / y)

    def print_name(self):
        print(self.name)

    def print_price(self):
        print(self.price)

c1 = Calculator("panpan", 100, 50, 60)

# 属性访问
print(c1.name)
print(c1.price)

# 成员方法访问
c1.print_name()
c1.print_price()
c1.add(1, 3)
c1.minus(1, 6)
c1.divide(1, 2)
c1.times(2, 3)

对于Python中的class类self和C++中的this指针类似(一个是指针一个是变量),还有__init__函数和构造函数

八,其他

1,zip的使用

当获取到2个列表或者元组数据,需要转换成一对一字典表时,可使用
注:
a–是拆包的意思
给列表拆包是一个

给字典拆包时2个**
如以下代码:
list3 = [1, 2, 3]
list4 = [4, 5, 6]

# 一对一字典
print(dict(zip(list3, list4)))

print(list(zip(list3, list4)))
for i, j in zip(list3, list4):
    print(i + j)

输出结果:
在这里插入图片描述

2,python中的%:

  1. 求模运算,相当于mod,也就是计算除法的余数,比如5%2就得到1。
  2. %还用在python的格式化输出,比如:

a = ‘test’
print ‘it is a %s’ %(a)
打印的结果就是 it is a test


注:本人Python小白一个,如果有错误还请大佬指出来,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值