【Python基础】适合小白的Python 简易入门指南(附代码)

0.导语

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

在此之前,我已经写了以下几篇AI基础的快速入门,本篇文章讲解python语言的基础部分,也是后续内容的基础。

本文代码可以在github下载:

https://github.com/fengdu78/Data-Science-Notes/tree/master/1.python-basic

文件名:Python_Basic.ipynb

1 Python数据类型

1.1 字符串

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

print ('hello world!')
hello world!
c = 'It is a "dog"!'
print (c)
It is a "dog"!
c1= "It's a dog!"
print (c1)
It's a dog!
c2 = """hello
world
!"""
print (c2)
hello
world
!
  • 转义字符''

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

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

原样输出引号内字符串可以使用在引号前加r

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

s = 'Python'
print( 'Py'in s)
print( 'py'in s)
True
False

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

print (s[2])
t
print (s[1:4])
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)
The first word is "hello", and the second word is "world"
hello world!

1.2 整数与浮点数

整数

Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样

i = 7
print (i)
7
7 + 3
10
7 - 3
4
7 * 3
21
7 ** 3
343
7 / 3#Python3之后,整数除法和浮点数除法已经没有差异
2.3333333333333335
7 % 3
1
7//3
2
浮点数
7.0 / 3
2.3333333333333335
3.14 * 10 ** 2
314.0

其它表示方法

0b1111
15
0xff
255
1.2e-5
1.2e-05

更多运算

import math

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

1.3 布尔值

True
True
False
False
TrueandFalse
False
TrueorFalse
True
notTrue
False
True + False
1
18 >= 6 * 3or'py'in'Python'
True
18 >= 6 * 3and'py'in'Python'
False
18 >= 6 * 3and'Py'in'Python'
True

1.4 日期时间

import time
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值