首页 > 分享 > Python 入门

Python 入门


Python是一种跨平台的计算机程序设计语言。是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。

1、Python数据类型

1.1 字符串
在Python中用引号引起来的字符集称之为字符串,比如:‘hello’、“my Python”、"2+3"等都是字符串 Python中字符串中使用的引号可以是单引号、双引号跟三引号

print ('hello world!') 1

hello world!

c = 'It is a "dog"!' print (c) 12

It is a “dog”!

c1= "It's a dog!" print (c1) 12

It’s a dog!

c2 = """hello world !""" print (c2) 1234

hello
world
!

转义字符
转义字符可以转义很多字符,比如n表示换行,t表示制表符,字符本身也要转义,所以 表示的字符就是

print ('It's a dog!') print ("hello world!nhello Python!") 12

It’s a dog!
hello world!
hello Python!
原样输出引号内字符串可以使用在引号前加r

print (r't') t 123 子字符串及运算

s = 'Python' print( 'Py' in s) print( 'py' in s) 123

True
False

取子字符串有两种方法,使用[]索引或者切片运算法[:],这两个方法使用面非常广

print (s[2]) 1

t

print (s[1:4]) 1

yth

字符串连接与格式化输出

word1 = '"hello"' word2 = '"world"' sentence = word1.strip('"') + ' ' + word2.strip('"') + '!' print( 'The first word is %s, and the second word is %s' %(word1, word2)) print (sentence) 123456

The first word is “hello”, and the second word is “world”
hello world!
1.2 整数与浮点数
整数
Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样

i = 7 print (i) 12

7

7 + 3 1

10

7 - 3 1

4

7 * 3 1

21

7 ** 3 1

343

7 / 3#Python3之后,整数除法和浮点数除法已经没有差异 1

2.3333333333333335

7 % 3 1

1

7//3 1

2
浮点数

7.0 / 3 1

2.3333333333333335

3.14 * 10 ** 2 1

314.0

其它表示方法

0b1111 1

15

0xff 1

255

1.2e-5 1

1.2e-05

更多运算

import math print (math.log(math.e)) # 更多运算可查阅文档 123

1.0

1.3 布尔值

True 1

True

False 1

False

True and False 1

False

True or False 1

True

not True 1

False

True + False 1

1

18 >= 6 * 3 or 'py' in 'Python' 1

True

18 >= 6 * 3 and 'py' in 'Python' 1

False

18 >= 6 * 3 and 'Py' in 'Python' 1

True

1.4 日期时间

import time now = time.strptime('2016-07-20', '%Y-%m-%d') print (now) 1234

time.struct_time(tm_year=2019, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=202, tm_isdst=-1)

time.strftime(

相关知识

Python数据分析入门之教你怎么搭建环境
Python入门习题大全——宠物
Python 入门
用Python入门知识点,打印自己的宠物小精灵,圆你的驯兽师梦
使用Python实现高效喂狗算法:从入门到进阶的编程技巧详解
【Python教程】教你用Python代码制作一个桌面宠物,专属桌宠,体验感升级1000%(附源码)
B站最受欢迎的Python教程,免费教学视频可以下载了
python——is和==区别
python入门:argparse浅析 nargs='+'
【Python程序】用200行Python代码制作有趣的桌面宠物(源码可分享),大打工人解压放松程序,如何用Python制作一个桌面宠物!

网址: Python 入门 https://m.mcbbbk.com/newsview666482.html

所属分类:萌宠日常
上一篇: 宠物狗如何带去越南,宠物狗如何带
下一篇: Pandas dtype obj