IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python 数据处理入门0 -> 正文阅读

[Python知识库]python 数据处理入门0

this file is derived from exercise 0

#1. Variables and data types

1.1 operator

Some relational operators:

SymbolTask Performed
==True, if both sides are equal
!=True, if both sides are not equal
<True, if the left side is smaller
>True, if the left side is greater
<=True, if the left side is smaller or equal to the right side
>=True, if the left side is greater or equal to the right side

1.2 basic functions

  • print()
x = "Hello"
y = "World"
print (x + y)
# "HelloWorld"
  • type(variable)
  • str(variable)
  • len(variable)

2. Data Structures

2.1 List

List can contain datas with same or different types, List is mutable.

lol = [[1,2,3], ["a","b","c"], [1.2,2.3,4.5,6.7,8.9]]
  • lol.append(value)
  • x = lol.pop(), pop out the last element of lol
  • lol.length()

2.2 Tuple

Tuples are just the same as lists, but are immutable.

t = (1,2,3)
t1 = ((1,2),(1,3),3)

2.3 Set

Sets are similar collections, but have no order and can contain each element only once.

s = set()
s.add(50)
s.add(20)
s.add(10)
s.add(20)
#s = {10, 20, 50} 
  • traverse all elements of a list and extract them to a set
l = [2,3,4,5,6,2,3,4,5,2]
s = set()
for x in l:
    s.add(x)
    
list (set(l))
#[2, 3, 4, 5, 6]

2.4 Dictionary

Python’s built-in mapping type. They map keys, which can be any immutable type, to values, which can be any type.

  • initialization 1st
presidents_inauguration = {}
presidents_inauguration ['Trump'] = 2017 
presidents_inauguration['Obama'] = 2009
presidents_inauguration['Bush'] = 2001
print(presidents_inauguration)
#{'Trump': 2017, 'Obama': 2009, 'Bush': 2001}
  • initialization 2nd
d = {'Trump': 2017, 'Obama': 2009, 'Bush': 2001}
  • keys, values
print (presidents_inauguration.keys())
#dict_keys(['Trump', 'Obama', 'Bush'])
print (presidents_inauguration.values())
#dict_values([2017, 2009, 2001])
  • len(dic)

2.5 Control Statements

control flow in Python noticeable does not use ANY (,),[,],{,},…
Instead indendation determines what belongs to block of commands

  • if-elif-else
x = 15
if x > 20:
    print ('This is a very big number!')
elif x > 10:
    print ('This is a big number!')
else:
    print ('this is a small number!')
  • loops
first_names = ['John', 'Paul', 'George', 'Ringo']
for name in first_names:
    print("Hello " + name + "!")
  • range function
    Contrary to many other programming languages there is no built-in for… counting loop.
    However, you can use the range function:
for x in range(10,25,5):
    print (x)
#10
#15
#20
  • enumerate
# enumerate is a useful convenience functions:
for index, name in enumerate (first_names):
    print("Name "+ str(index) + ": " + name)
# Name 0: John
# Name 1: Paul
# Name 2: George
# Name 3: Ringo
  • while loop
# while loops functions very similar to many popular languages:
x = 1
while x < 100:
    x = x * 2
    print(x)

2.6 Functions

  • definition
def print_all_names(names):
    for x in names:
        print (x)
  • lambda function

Lambda functions are a concise way of defining functions, e.g.:

f = lambda x: x*5 + 1
f(10)

2.7 Import

Python has a lot of built-in packages you can use, or you can download and install more packages from the internet.
Using such packages is easy:

import antigravity
import statistics
statistics.mean([3,5,7,9])
# 6

# you can also import just single functions from a package
from math import log
log (2.71 * 2.72)
# 1.9975805151995156

2.8 Exceptions

def to_int_safe (x):
    try:
        z = int (x)
    except:
        print ("We cannot convert everything like this!")
        z = -1
    return z

to_int_safe("5.5")
# We cannot convert everything like this!
# -1

2.9 File Handling

  • open for reading
with open("filename", "r") as f: #r ==> open for reading
    for line in f:
        print (line.strip())
  • open for writing
my_list = ["a", "bc", "de"]
with open("fienae.txt", "wb") as f: #r ==> open for writing
    for element in my_list:
        f.write (element + "\n")
  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-10-23 12:25:47  更:2021-10-23 12:26:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/29 11:58:34-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计