当前位置: 首页 > article >正文

python起步

环境:redhat8  python3.6

交互式python
[root@rhel8 ~]# python3
Python 3.6.8 (default, Dec  5 2019, 15:45:45) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> exit()
python文件
[root@rhel8 ~]# mkdir day01
[root@rhel8 ~]# cd day01/
[root@rhel8 day01]# vim demo01.py
print("hello world")
print("hello"+" world") #首尾相连
print(100+200)  #没有引号,数字运算
print("hello"+100) #会报错
[root@rhel8 day01]# python3 demo01.py 
hello world
hello world
300
Traceback (most recent call last):
  File "demo01.py", line 4, in <module>
    print("hello"+100) #会报错
TypeError: must be str, not int
[root@rhel8 day01]# cat demo01.py 
print("hello world")
print("hello"+" world") #首尾相连
print(100+200)  #没有引号,数字运算
#print("hello"+100) #会报错
print("100"+"200") #100200
#打印多组数据
print("hao",123,"hello") #hao 123 hello 输出中间有空格
#seq:表示多个元素中的分隔符,默认是空格
print("hao",123,"hello",sep="++") #hao++123++hello 分隔符换成了++
#根据输出结果也会发现,每次print语句都会换行,这是因为end参数
print("hello world",end="!!!")#结束符换成!!!,输出看结果。
#hello world!!!hello world
print("hello world")
[root@rhel8 day01]# python3 demo01.py 
hello world
hello world
300
100200
hao 123 hello
hao++123++hello
hello world!!!hello world
input()函数
[root@rhel8 day01]# python3
Python 3.6.8 (default, Dec  5 2019, 15:45:45) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input()  #通过input()从键盘获取的值,一定是字符串类型的
你好
'你好'
>>> input("username:")
username:zhangsan
'zhangsan'
>>> user = input("username:")
username:zhangsan
>>> print(user)
zhangsan
>>> print("user")
user
>>> print("hello",user)
hello zhangsan
>>> number = input("number:")
number:10
>>> print(number)
10
>>> number + 5  #input()获取的值都是字符类型,字符串和数字不能参与运算
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int
>>> int(number) + 5  #将number变量的值,转换成int类型,整数类型的俩个值可以相互运算
15
>>> number + "5"  #数字类型转换为字符串,并进行拼接操作
'105'
要求写一个input()接收zhangsan,输出Welcome zhangsan的login.py
[root@rhel8 day01]# cat login.py 
username =  input("username:")
print("Welcome",username)
[root@rhel8 day01]# python3 login.py 
username:zhangsan
Welcome zhangsan
[root@rhel8 day01]# python3 login.py 
username:lisi
Welcome lisi
指定python解释器
[root@rhel8 day01]# /usr/bin/python3
Python 3.6.8 (default, Dec  5 2019, 15:45:45) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@rhel8 day01]# cat login.py 
#! /usr/bin/python3  
#指定python3
username =  input("username:")
print("Welcome",username)
[root@rhel8 day01]# chmod +x login.py 
[root@rhel8 day01]# ./login.py 
username:zhangsan
Welcome zhangsan
变量

变量定义

在Python中,每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建

等号(=)用来给变量

 

     = 左边是一个变量名

     = 右边是存储在变量中的值

变量名 = 值

 变量定义之后,后续就可以直接使用了

vim demo1.py
account = '3186629509@qq.com'
password = '123456'

print(account)
print(password)

[root@rhel8 mypython]# python3 demo1.py 
3186629509@qq.com
123456
练习1

买包子(变量:单价,数量,总价)

 可以用其他变量的计算结果来定义变量,变量定义之后,后续就可以直接使用了

需求:

包子的价格是1.5元/个

买了10个包子

计算付款金额

[root@rhel8 mypython]# vim demo2.py
price = 1.5
num = 10
total = num * price
print("money is",total)
[root@rhel8 mypython]# python3 demo2.py 
money is 15.0
练习2

买包子进阶

今天老板高兴,总价打9折,请重新计算购买金额

[root@rhel8 mypython]# vim demo2.py
price = 1.5
num = 10
total = (num * price)
total = total * 0.9
print("money is",total)
[root@rhel8 mypython]# python3 demo2.py 
money is 13.5

提问:

上述代码中,一共定义有几个变量?

   三个:price/num/total

total = total * 0.9,是在定义新的变量还是在使用变量?

   直接使用之前已经定义的变量

   变量名只有在第一次出现才是定义变量

   变量名再次出现,不是定义变量,而是直接使用之前定义过的变量

在程序开发中,可以修改之前定义变量中保存的值吗?

  可以,变量中存储的值,就是可以变的

算术运算符
运算符描述实例
+10 + 20 = 30
10 - 20 = -10
10 * 20 = 200
/10 / 20 = 0.5
//取整除返回除法的整数部分(商)  9 // 2输出结果4
%取余数

返回除法的余数9 % 2 = 1

** 又称次方、乘方,2 ** 3 = 8
[root@rhel8 mypython]# python3
Python 3.6.8 (default, Dec  5 2019, 15:45:45) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> 10 - 20
-10
>>> 10 * 20
200
>>> 10 / 20
0.5
>>> 20 // 3
6
>>> 20 %  3
2
>>> 2 ** 3
8
比较运算符
运算符描述
==检查俩个操作数的值是否相等,如果是,则条件成立,返回True
!=检查俩个操作数的值是否不相等,如果是,则条件成立,返回True 1 != 2 --> True
>检查左操作数的值是否大于右操作数的值,如果是,则条件成立,返回true
<检查左操作数的值是否小于右操作数的值,如果是,则条件成立,返回true
>=检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立,返回true
<=检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立,返回true
>>> 5 > 3
True
>>> 10 < 15 > 13
True
>>> 10 < 15 and 15 > 3
True
逻辑运算符
运算符逻辑表达式描述
andx and y只有x和y的值都为True,才会返回True。否则只要x或者y有一个为False,就返回False
orx or y只要x或者y有一个值为True,就返回True。只有x和y的值都为False,才会返回False
notnot x如果x为True,返回False;如果x为False,返回True
>>> 10 > 5 and 5 > 3
True
>>> 10 > 5 or 5 > 30
True
>>> 10 > 50
False
>>> not 10 > 50
True

数据类型

数字

基本的数字类型有:

   int: 有符号整数

   bool: 布尔值

               True: 1;False: 0

   float浮点数

>>> type(5)
<class 'int'>
>>> type(1.0)
<class 'float'>
>>> 5 + 3
8
>>> 5 + 3.2
8.2
>>> True + 1
2
>>> False + 1
1
>>> False * 8
0
>>> hex(200)   函数hex(),可以将十进制数200,转换为16进制数
'0xc8'
>>> oct(200)   函数oct(),可以将十进制数200,转换为8进制数
'0o310'
>>> bin(200)   函数bin(),可以将十进制数200,转换为2进制数
'0b11001000'
字符串

Python中字符串被定义为引号之间的字符集合

>>> words = """
... abc
... def
... ghj
... """
>>> words
'\nabc\ndef\nghj\n'
>>> print(words)

abc
def
ghj


http://www.kler.cn/news/160247.html

相关文章:

  • 问卷调查须避免的错误要点(02):避免逻辑错误与提升数据质量
  • 基于jsp+servlet+mybatis的简易在线选课系统
  • Dubbo(二)dubbo调用关系
  • golang使用sip协议 用户名和密码注册到vos3000
  • vue3中如何实现事件总线eventBus
  • 【数据结构(八)】哈希表
  • OpenCV-python numpy和基本作图
  • 甘草书店:#8 2023年11月22日 星期三「“说一套做一套”的甘草与麦田」
  • InnoDB的数据存储结构
  • Qt5.15.2的镜像网址
  • 用100ask 6ull配合 飞凌 elf1的教程进行学习的记录 - ap3216
  • SQL手工注入漏洞测试(Sql Server数据库)-墨者
  • 【Linux】进程控制-进程终止
  • 【musl-pwn】msul-pwn 刷题记录 -- musl libc 1.2.2
  • 面试官问:如何手动触发垃圾回收?幸好昨天复习到了
  • HarmonyOS学习--创建和运行Hello World
  • 基于SSM的物资物流系统
  • 什么是呼叫中心的语音通道?呼叫中心语音线路有几种?
  • [Electron] 将应用日志文件输出
  • 图解系列--Web服务器,Http首部
  • 我想涨工资,请问测试开发该怎么入门?
  • Zabbix自定义飞书webhook告警媒介2
  • vue 过滤器 (filters) ,实际开发中的使用
  • 解决 video.js ios 播放一会行一会不行
  • 【技术分享】RK356X Android11 以太网共享4G网络
  • Gti GUI添加标签
  • IPv4/IPv6 组播对应的MAC地址
  • Scala--2
  • 智能优化算法应用:基于蜜獾算法无线传感器网络(WSN)覆盖优化 - 附代码
  • 一篇文章带你详细了解C++智能指针