python入门合集:

python快速入门【一】-----基础语法

python快速入门【二】----常见的数据结构

python快速入门【三】-----For 循环、While 循环

python快速入门【四】-----各类函数创建

python快速入门【五】---- 面向对象编程

python快速入门【六】----真题测试


python快速入门【一】-----基础语法

IPython/Jupyter

IPython Notebook号称 “编码器的实验室笔记本” - 允许用户在单个基于浏览器的页面中交叉显示/执行数据,代码和说明文本,而不是在单独的文件中

基础类型

python中的主要基本数据类型是数字(整数和浮点数),布尔值和字符串

Hello World!

In [1]

# This is a one line comment
print('Hello World!')
Hello World!

In [2]

# Strings can be enclosed by ',", or """
print("Hello World!")
Hello World!

In [21]

# \ is used as a escape character.
# There are many special escape sequences
# Notable are: \t (tab)
# \n (new line)

In [22]

print("The \n makes a new line")
The
makes a new line

In [23]

print("The \t is a tab")
The 	 is a tab

In [24]

print('I\'m going to the movies')
I'm going to the movies

In [25]

# Using \ to not accidently close the string by having a closing "
print("This is a string enclosed by \"\" not '' ")
This is a string enclosed by "" not ''

In [3]

# Creating a variable
# Variables are used to store information to be referenced
# and manipulated in a computer program. firstVariable = 'Hello World'
print(firstVariable)
Hello World

字符串操作

字符串是python的特殊类型。作为对象,在类中,您可以使用.methodName()表示法调用字符串对象上的方法。字符串类在python中默认可用,因此您不需要import语句即可将对象接口用于字符串。

In [27]

# go over ? mark after if you are not sure what method does.
print(firstVariable.lower())
print(firstVariable.upper())
print(firstVariable.title())
hello world
HELLO WORLD
Hello World

In [28]

# To look up what each method does
firstVariable.lower?

In [35]

# Can also use help
help(firstVariable.lower)
Help on built-in function lower:

lower(...)
S.lower() -> string Return a copy of the string S converted to lowercase.

In [36]

help

In [29]

firstVariable.split(' ')
['Hello', 'World']

In [4]

a=firstVariable.split(' ')
a
['Hello', 'World']

In [5]

' '.join(a)
'Hello World'

In [32]

print("0" + "1")
01

In [6]

"0" * 3
'000'

In [7]

# You can add strings together.
"Fizz" + "Buzz"
'FizzBuzz'

基础数学

有四种不同的数字类型:普通整数,长整数,浮点数和复数。另外,布尔值是普通整数的子类型。

In [138]

# Addition, add two int together
1+1
2

In [139]

# if one of the operands is a float, the result is a float.
130-2.0
128.0

In [140]

130-2
128

In [141]

# integer division
130/2
65.0

In [142]

130.0/2
65.0

In [143]

# Multiplication
2*3
6

In [144]

# Exponentiation **
# This operator raises the number to its left to the power of the number to its right
2**3
8

In [145]

# Modulo
# Returns the remainder of the division of the number to the left by the
# number on its right.
9%3
0

if 语句

比较操作符 功能
< 小于
<= 小于或等于

| 大于 = | 大于或等于 == | 等于 != | 不等于

检查某些东西是否为True,如果是,则执行此操作。如果它不是True(False),则不执行

In [8]

# Notice you have to indent after you start a if statement.
num = 3
if num == 3:
print(num)
3

In [147]

# Nothing is outputted because num > 10 is FALSE
num = 3
if num > 10:
print(num)

In [148]

num = 3
if num % 3 == 0:
print("Fizz")
Fizz

In [149]

num = 10
if num % 5 == 0:
print("Buzz")
Buzz

In [150]

if True:
print("This was True")
This was True

In [151]

if False:
print("Nothing printed")
逻辑操作符 描述
and 如果两个操作数均为True,则condition变为True.
or 如果两个操作数中的任何一个为True,则condition变为True.
not 用于反转逻辑(不是False变为True,而不是True变为False

In [152]

num = 4
num > 0 and num < 15
True

In [153]

# both the conditions are true, so the num will be printed out
if num > 0 and num < 15:
print(num)
4

In [154]

# num > 0 is True, num > 15 is False
# Since the first condition is True, it is True
num = 4
num > 0 or num > 15
True

In [155]

if num > 0 or num  > 15:
print(num)
4

In [156]

# or will only evaluate to False if both are False
if False or False:
print('Nothing will print out')

In [157]

num = 10
not num < 20
False

else 语句

必须在if或elif语句之后。最多可以有一个其他声明。仅当上面的所有“if”和“elif”语句都为False时才会执行

In [158]

num = 1
if num > 3 :
print("Hi")

In [159]

"""We will execute what is inside the else statement
because num is not greater than 3
"""
num = 1
if num > 3 :
print("Hi")
else:
print("number is not greater than 3")
number is not greater than 3

In [160]

"""We will execute what is inside the if statement because num > 4"""
num = 4
if num > 3 :
print("Hi")
else:
print("number is not greater than 3")
Hi

Task

  1. 将num分配给整数值。
  2. 如果整数是偶数,写一个if else组合将打印“你的整数是偶数”。否则,打印“你的整数是奇数”。

提示:任何可以精确地除以2的整数都是偶数(例如:2,4,6)。任何不能精确地除以2的整数都是奇数(例如:1,3,5)。使用模运算符(%),它将数字左边的余数除以右边的数字。

In [161]

num = 3
if num % 2 == 0:
print("Your integer is even")
else:
print("Your integer is odd")
Your integer is odd

elif 语句

必须在if语句之后。 elif语句语句允许您检查True的多个表达式,并在其中一个条件求值为True时立即执行代码块。

与else类似,elif语句是可选的。但是,与其他情况不同,最多只能有一个语句,if后面可以有任意数量的elif语句。

In [162]

num = 21
if num > 50:
print('num is larger than 50')
elif num == 21:
print('num = 21')
else:
print('Catchall condition')
num = 21

In [163]

my_num = 5
if my_num % 2 == 0:
print("Your number is even")
elif my_num % 2 != 0:
print("Your number is odd")
else:
print("Are you sure your number is an integer?")
Your number is odd

In [164]

# You can have mulitple elif statements.
# Remember only the first True statement has its block of code executed. dice_value = 1
if dice_value == 1:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 2:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 3:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 4:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 5:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 6:
print('You rolled a {}. Great job!'.format(dice_value))
else:
print('None of the conditions above (if elif) were evaluated as True')
You rolled a 1. Great job!

Task

  1. 将num分配给整数值。
  2. 编写一系列if,elif,else语句,打印您指定的num。但是对三的倍数要打印“Fizz”而不是数字, 五的倍数要打印“Buzz”。对于三和五共同的倍数则打印“FizzBuzz”

In [165]

# Solution 1
num = 10

In [166]

if num % 3 == 0 and num % 5 == 0:
print('FizzBuzz')
elif num % 3 == 0:
print('Fizz')
elif num % 5 == 0:
print('Buzz')
else:
print(str(num))
Buzz

In [167]

# Solution 2
num = 15

In [168]

"""Notice how difficult this solution is relative to Solution 1"""

string = ""
if num % 3 == 0:
string = string + "Fizz"
if num % 5 == 0:
string = string + "Buzz"
if num % 5 != 0 and num % 3 != 0:
string = string + str(num)
print(string)
FizzBuzz

列表

列表后面要加上方括号 [ ]

| | | | --- | --- | --- | --- | --- z =| [3, | 7, | 4, | 2] index | 0 | 1 | 2 | 3

In [2]

# Defining a list
z = [3, 7, 4, 2]

访问列表里面的值

In [3]

# The first element of a list is at index 0
z[0]
3

In [4]

z[2]
4

In [6]

# Access Last Element of List
z[-2]
4

切分列表

In [120]

# first index is inclusive (before the :) and last (after the :) is not.
# not including index 2
z[0:2]
[3, 7]

In [22]

# everything up to index 3
z[:3]
[3, 7, 4]

In [47]

# index 1 to end of list
z[1:]
[7, 4, 2]

取列表的最大值, 最小值, 长度, 以及总和

In [7]

print(min(z), max(z), len(z), sum(z))
2 7 4 16

对列表中对象出现次数进行统计

In [8]

random_list = [4, 1, 5, 4, 10, 4]
random_list.count(4)
3

返回列表第一个指针

| | | | | | --- | --- | --- | --- | --- | --- | --- random_list =| [4, | 1, | 5, | 4, | 10, | 4] index=| 0 | 1 | 2 | 3 | 4 | 5

In [9]

random_list.index(4)
0

In [75]

# you can specify where you start your search
random_list.index(4, 3)
3

In [180]

# random_list.index(value, [start, stop])
random_list.index(4, 5, 6)
5

对列表进行排序

In [15]

x = [3, 7, 2, 11, 8, 10, 4]
y = ['Steve', 'Rachel', 'Michael', 'Adam', 'Monica', 'Jessica', 'Lester']

In [16]

# Sorting and Altering original list
# low to high
x.sort()
print(x)
[2, 3, 4, 7, 8, 10, 11]

In [17]

# Sorting and Altering original list
# high to low
x.sort(reverse = True)
print(x)
[11, 10, 8, 7, 4, 3, 2]

In [18]

# Sorting and Altering original list
# A-Z
y.sort()
print(y)
['Adam', 'Jessica', 'Lester', 'Michael', 'Monica', 'Rachel', 'Steve']

In [19]

# Sorting and Altering original list
# Z-A
y.sort(reverse = True)
print(y)
['Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']

In [20]

# sorting list WITHOUT altering original list
new_list = sorted(y)
new_list
['Adam', 'Jessica', 'Lester', 'Michael', 'Monica', 'Rachel', 'Steve']

In [21]

# notice y is unchanged
y
['Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']

在列表结尾添加一个对象

In [22]

x
[11, 10, 8, 7, 4, 3, 2]

In [23]

x.append(3)
print(x)
[11, 10, 8, 7, 4, 3, 2, 3]

删除列表中一个对象

In [24]

x.remove(10)
print(x)
[11, 8, 7, 4, 3, 2, 3]

删除列表中指定位置的对象

In [25]

# List before you remove an item
print(x)
[11, 8, 7, 4, 3, 2, 3]

In [26]

# Remove item at the index
# this function will also return the item you removed from the list
# Default is the last index
x.pop(3)
4

In [27]

print(x)
[11, 8, 7, 3, 2, 3]

合并列表

通过在末尾续加的方式来延长列表

In [28]

x.extend([4, 5])

In [29]

x
[11, 8, 7, 3, 2, 3, 4, 5]

In [195]

# lists can be diverse, each element in the list can be of a different type.
# lists are really list of pointers, and these pointers can
# point to anything. # Concatenating Lists
print('x+y=',x+y)
x+y= [11, 8, 7, 3, 2, 3, 4, 5, 'Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']

在列表指定位置前插入对象

In [30]

x
[11, 8, 7, 3, 2, 3, 4, 5]

In [197]

x.insert(4, [4, 5])

In [198]

x
[11, 8, 7, 3, [4, 5], 2, 3, 4, 5]

python快速入门【一】-----基础语法的更多相关文章

  1. JavaScript快速入门-ECMAScript基础语法

    一.JavaScript引入方式 1.行内式 <script> alert(123); </script> 2.外链式 <script src='custom.js'&g ...

  2. python快速入门——进入数据挖掘你该有的基础知识

    这篇文章是用来总结python中重要的语法,通过这些了解你可以快速了解一段python代码的含义 Python 的基础语法来带你快速入门 Python 语言.如果你想对 Python 有全面的了解请关 ...

  3. Python入门篇-基础语法

    Python入门篇-基础语法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编程基础 1>.程序 一组能让计算机识别和执行的指令. 程序 >.算法+ 数据结构= 程 ...

  4. Python快速入门

    Python快速入门 一.基础概要 命名:h.py Linux命令行运行:python h.py 注释.数字.字符串: 基本类型只有数字与字符串 #python注释是这样写的 ''' 当然也可以这样 ...

  5. Python快速入门PDF高清完整版免费下载|百度云盘

    百度云盘:Python快速入门PDF高清完整版免费下载 提取码:w5y8 内容简介 这是一本Python快速入门书,基于Python 3.6编写.本书分为4部分,第一部分讲解Python的基础知识,对 ...

  6. Git 快速入门--Git 基础

    Git 快速入门 Git 基础 那么,简单地说,Git 究竟是怎样的一个系统呢? 请注意接下来的内容非常重要,若你理解了 Git 的思想和基本工作原理,用起来就会知其所以然,游刃有余. 在开始学习 G ...

  7. python快速改造:基础知识

    改造"Hacking"并不同于破坏"cracking" python快速改造:基础知识 一行就是一行,不管多少,不用加分号 交互式python解释器可以当作计算 ...

  8. python快速入门及进阶

    python快速入门及进阶 by 小强

  9. Python入门 —— 02基础语法

    基础语法入门学习推荐: 简明 Python 教程 下文仅为入门推荐书籍的补充与重点 多行语句:末尾使用斜杠 (  ) ,将一行分为多行 var = item1 + item2 + item3 注释: ...

  10. Python与C语言基础对比(Python快速入门)

    代码较长,建议使用电脑阅读本文. 10分钟入门Python 本文中使用的是Python3 如果你曾经学过C语言,阅读此文,相信你能迅速发现这两种语言的异同,达到快速入门的目的.下面将开始介绍它们的异同 ...

随机推荐

  1. Midjouney限时免费体验

    前言 Midjourney 是一个人工智能程序,可根据文本生成图像,目前架设在 Discord 频道上.于 2022 年 7 月 12 日进入公开测试阶段,使用者可通过 Discord 的机器人指令进 ...

  2. COGI控制删除按钮增强

    1.业务需求 COGI在SAP中主要用于解决生产订单异常的问题.通常发生在生产订单执行过程中,当出现了特定的异常情况时,需要进行处理.这些异常情况可能包括物料缺失.数量不匹配.质量问题等等. 当这些异 ...

  3. 汇编 | CPU物理地址本质理解

    物理地址 我们知道,CPU访问内存单元时,要给出内存单元的地址.所有的内存单元构成的存储空间是一个一维的线性空间,每一个内存单元在这个空间中都有唯一的地址,我们将这个唯一的地址称为物理地址. CPU通 ...

  4. # 0x56 动态规划-状态压缩DP

    0x56 动态规划-状态压缩DP Mondriaan's Dream Description Squares and rectangles fascinated the famous Dutch pa ...

  5. 2014年第五届蓝桥杯【C++省赛B组】

    第一题:啤酒和饮料 啤酒每罐2.3元,饮料每罐1.9元.小明买了若干啤酒和饮料,一共花了82.3元. 我们还知道他买的啤酒比饮料的数量少,请你计算他买了几罐啤酒. 注意:答案是一个整数.请通过浏览器提 ...

  6. C#将日期格式化为指定格式

    private void btn_GetTime_Click(object sender, EventArgs e) { lab_time.Text = DateTime.Now.ToString(& ...

  7. Go语言安装(Windows10)

    一. 官网下载 https://golang.google.cn/dl/   二. 软件包安装 选择对应的路径进行安装   三. 环境变量设置 1.path 检查系统环境变量Path内已经添加Go的安 ...

  8. Jstack 查看线程状态及定位占用 cpu 较高的 java 线程

    本文为博主原创,未经允许不得转载: 1. Jstack 用来查看 java 指定进程所包含的 java 线程状态:    "arthas-NettyHttpTelnetBootstrap-3 ...

  9. Nginx Location 深入剖析及动静分离简易配置

    本文为博主原创,未经允许不得转载: 1. location 使用分析 location 是 Nginx 配置 中的一个指令,用于访问的 URL 匹配,而在这个 location 中所配置的每个指令将会 ...

  10. Linux 中常用的基础命令

    by emanjusaka from https://www.emanjusaka.top/2024/01/linux-base-command 彼岸花开可奈何 本文欢迎分享与聚合,全文转载请留下原文 ...