#######python基础#########

本文详细介绍了Python编程的基础知识,包括变量的使用、数据类型及其转换、输入输出操作、格式化输出等核心内容。通过实例演示了如何在Python中处理不同类型的数据,并展示了如何利用格式化输出进行数据展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1第一个python程序
在这里插入图片描述
2.python中的变量
b = 1234
a = b
print(a)
print(b)
在这里插入图片描述
##变量就是引用

在内存中删除一个变量

 >>> del a
 >>> a
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 NameError: name 'a' is not defined
 defined


"""
块注释
qq_passwd=45678
print(qq_passwd)

"""

3.python中的数据类型
1 . 整型

 >>> a = 1
 >>> print(a)
 1
  1. 查看变量的数据类型

    >>> type(a)
    <class 'int'>
    
  2. 浮点型

      >>> b=1.2
      >>> print(b)
      #1.2
      >>> type(b)
      <class 'float'>
    
  3. 字符串型

    >>> c='westos'
    >>> print(c)
    westos
    >>> type(c)
    <class 'str'>
    ##字符串必须加''框住
    
  4. bool型(只有两个值:True False 非0即真)

    >>> a = 1
    >>> bool(a)
    True
    >>> bool(0)
    Fals
    ####################
    >>> bool(' ')
    True
    >>> bool('')
    False
    ####################
    >>> bool('redhat')
    True
    >>> bool(1234567)
    True
    ##非0非空即真
    

6.数据类型的转换

>>> a = 1
>>> type(a)
<class 'int'>
>>> float(a)
1.0
type(a)
<class 'int'>
##更改值是临时的
>>>b=2,0
>>> print(b)
(2, 0)
>>> b=2.0
>>> int(b)
2
####################################
>>> c='linux'
>>> int(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'linux'
##字母不能强行变成数字
######################################
>>> c='123'
>>> int(c)
123
>>> b=345
>>> str(b)
'345'
>>> e=12.6
>>> str(e)
'12.6'

4.python中的输入与输出
#输入
#python2.x
#input:只支持数值类型
#raw_input:支持数值类型和字符串类型

>>> input('Num:')
Num:123
123
>>> input('Num:')
Num:abc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
>>> raw_input('Num:')
Num:westos
'westos'
>>> raw_input('Num:')
Num:123
'123'

#希望输入密码不回显

 >>> import getpass  #导入python第三方模块库
 >>> raw_input('请输入密码:')
 请输入密码:123
 '123'
 >>> num = getpass.getpass('请输入密码:')
 请输入密码:
 >>> num
 '123'

#python3.x
#没有raw_input()
#input():接收任意数据类型

 >>> num = input('Num:')
 Num:123
 >>> num = input('Num:')
 Num:redhat
 >>> num
 'redhat'
 >>> num = raw_input('Num:')
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 NameError: name 'raw_input' is not defined

如果接收到的数值要进行比较的时候,一定要转换为同种数据类型

>>> age = raw_input('请输入年龄:')
请输入年龄:18
>>> age
'18'
>>> age > 19
True
>>> type(age)
<type 'str'>
>>> int(age)
18
>>> age > 19
True
>>> int(age)>19
False

5.python中的格式化输出
1.%s:代表字符串 %d:整型

>>> name = 'westos'
>>> age=11
>>> print('%s的年龄为%d' %(name,age))
westos的年龄为11
>>> name = 'redhat'
>>> print('%s的年龄为%d' %(name,age))
redhat的年龄为11
>>> age=18
>>> print('%s的年龄为%d' %(name,age))
redhat的年龄为18
>>> age='20'
>>> print('%s的年龄为%d' %(name,age))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
>>> age=20
>>> name=123
>>> print('%s的年龄为%d' %(name,age))
123的年龄为20

2.%f:代表浮点型
#%.xf(x:2 3 4 5)保留小数点后多少位

>>> money=32321.4343243
>>> name='tom'
>>> print('%s本月的工资为%f' %(name,money))
tom本月的工资为32321.434324
>>> money=7000
>>> print('%s本月的工资为%f' %(name,money))
tom本月的工资为7000.000000
>>> print('%s本月的工资为%.2f' %(name,money))
tom本月的工资为7000.00
>>> print('%s本月的工资为%.3f' %(name,money))
tom本月的工资为7000.000
>>> print('%s本月的工资为%.4f' %(name,money))
tom本月的工资为7000.0000
>>> print('%s本月的工资为%.1f' %(name,money))
tom本月的工资为7000.0

3.整型总占位,不够位数的前面补0

>>> sid = 1
>>> name='lily'
>>> print('%s的学号为%d' %(name,sid))
lily的学号为1
>>> print('%s的学号为130%d' %(name,sid))
lily的学号为1301
>>> print('%s的学号为%.5d' %(name,sid))
lily的学号为00001
>>> sid=002
File "<stdin>", line 1
sid=002          ^
SyntaxError: invalid token
>>> sid = 123
>>> print('%s的学号为%.5d' %(name,sid))
lily的学号为00123
>>> sid = 12345
>>> print('%s的学号为%.5d' %(name,sid))
lily的学号为12345
>>> print('%s的学号为%.8d' %(name,sid))
lily的学号为00012345

4.百分数的实现

>>> scale = 0.1
>>> print('数据的比例是:%.2f' %(scale * 100))
数据的比例是:10.00>>> print('数据的比例是:%.2f%' %(scale * 100))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print('数据的比例是:%.2f%%' %(scale * 100))
数据的比例是:10.00%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值