python学习笔记1基础知识

1.首先,下载python3.7.4
注释符号 :#
输出函数:
print(num) #输出数num
print(“str”) #输出字符串或字符str
print(“str1”+“str2”)
print(表达式)
print(“str” * num) #输出num个str
变量不需要声明,直接给一个合法的名字赋值,变量就产生了
2.
函数舍弃大括号{ },小括号,使用冒号和缩进
例如

if  a>b:
	print(a)
else:
	print(b)

input函数
a=input(“input a number:”) #控制台会显示“input a number”input输入的是字符串或者字符
str=input(“input a string:”)
输入的数字字符可强制变为数字
但是字母字符不能变为数字

>>> a=input()
1234
>>> a
'1234'
>>> b=int(a)
>>> b
1234
>>> 

2.内置函数BIF
可直接调用的函数,加快编程速度。
例如
print()
input()
help(BIF) #用于显示BIF的功能描述
在IDLE中输入dir(builtins)可以看到python提供的内置函数列表

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 
'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 
'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 
'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 
'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 
'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 
'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 
'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 
'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 
'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 
'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 
'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 
'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 
'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 
'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', 
'__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 
'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 
'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter',
 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 
 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 
 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 
 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 
 'zip']
>>> 

3.字符串
3.1
字符串可以进行相加,但不能相减
字符串可以乘以正整数

>>> a="acbdv"
>>> b="bcd"
>>> a-b
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    a-b
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>> a+b
'acbdvbcd'
>>> a*3
'acbdvacbdvacbdv'

3.2
转义字符(\)
转义\后面的字符
例如\n

>>> print("c:\\n语言")
c:\n语言
>>> 

3.3
原始字符串:使代码仅以字符输出
方法:在字符串前面加一个r

>>> string=r"c\n语言"
>>> print(string)
c\n语言
>>> 

注意:无论是否原始字符串都不以反斜杠\作为结尾(反斜杠放在字符串的末尾表示该字符串还灭有结束,换行继续的意思)如果坚持这样做就会报错

>>> b=adhd\

   
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    b=adhd\
NameError: name 'adhd' is not defined
>>> 

可以在反斜杠前再加一个反斜杠进行转义

>>> a='abcd\\'
>>> print(a)
abcd\
>>> 

3.4
长字符串
适合定义行数多的字符串

>>> abd="""123
12345
1234567
"""
>>> print(abd)
123
12345
1234567

>>> abd
'123\n12345\n1234567\n'
>>> 

三个引号括起来,然后换行输入,就可以了
3.5 条件分支
if函数
if 条件:
条件为真时的操作
else:
条件为假时的操作

if  a>b:
	print(a)
else:
	print(b)

while 循环
while 条件:
条件为真时的操作

n=4
while n>0:
		print(n)
		n-=1

3.6引入模块
我们编写的每一个程序其实都是一个模块
import 模块名
模块名.模块函数()
例如引入random模块

import random
num=4
while num>=0:
    a=random.randint(1,10)
    print(a)
    num-=1

结果

5
7
1
4
9
>>>

3.7数据类型
3.7.1
整型:python3的整型和长整型进行了无缝结合,使它的长度不受限制(或者说只受限于计算机的虚拟内存总数)所以用python很容易进行大数计算
浮点型:小数
布尔类型:Ture=1 False=0
3.7.2
类型转换
int(),float(),str()
int()作用是将一个字符串或浮点数转换为一个整数
注意:如果浮点数转换为整数,小数点后面数据会被直接砍掉,不进行四舍五入;
float() 作用是将一个字符串或者整数转换为浮点数
str()的作用是将一个数或者任何其他类型转换为一个字符串

>>> c=str(5e15)
>>> c
'5000000000000000.0'
>>> 

3.7.3获得数据类型
1.type()函数可获得变量的数据类型

>>> type(c)
<class 'str'>
>>> 

2.isinstance()函数,可根据所给的两个参数一个是待确定的变量,一个是指定的数据类型,返回的布尔类型来确定变量类型

>>> isinstance(c,str)
True
>>> isinstance(c,int)
False
>>> 

3.8
3.8.1常用运算符
与C语言和c++相比,没有了++,–,增加了//,**
/变为了真正的除法,不再是整数相除只取整数部分
//则满足原来的需求,不过若是浮点数使用//也执行只取整数的操作
幂运算符,左侧为底数,右侧为指数
如3**2=9
幂运算符比其左侧一元操作符高,比其右侧一元操作符低

>>> 5/4
1.25
>>> 5//4
1
>>> 5.3/2
2.65
>>> 5.3//2
2.0
>>> 5**2
25
>>>-5**2
-25
>>>5**-2
0.04

3.8.2
逻辑操作运算符 and not or
表达式3<4<5在python中合法,

>>> 3<4<5
True
>>>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值