使用Google Colab的python教程

本文是一篇关于使用Google Colab进行Python编程的教程,涵盖了Python的基础知识,包括检查Python版本、数据类型(如int、float、bool、string)、容器(如list、dict、set、tuple)、函数、类以及Numpy的使用。通过示例展示了列表操作、字典的键值对、集合的特性以及如何创建和操作Numpy数组。
摘要由CSDN通过智能技术生成

原文链接https://cs231n.github.io/python-numpy-tutorial/Python Tutorial With Google Colab

检查python版本

!python --version

经典快速排序

def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)

print(quicksort([3,6,8,10,1,2,1]))

数据类型

  • int

x = 3
print(x, type(x))

3 <class ‘int’>

  • float

y = 2.5
print(type(y))
print(y, y + 1, y * 2, y ** 2)

<class ‘float’>
2.5 3.5 5.0 6.25

  • Booleans

t, f = True, False
print(type(t))

<class ‘bool’>

print(t and f) # Logical AND;
print(t or f) # Logical OR;
print(not t) # Logical NOT;
print(t != f) # Logical XOR;

False
True
False
True

  • strings

hello = 'hello' #String literals can use single quotes
world = ''world" # or double quotes, it does not matter
print(hello, len(hello))

hello 5

hw = hello + ' ' + world # String concatenation print(hw)

hello world

hw12 = '{} {} {}'.format(hello, world, 12) # string formatting print(hw12)

hello world 12

String objects have a bunch of useful methods; for example:

s = "hello"
print(s.capitalize())  # Capitalize a string
print(s.upper())       # Convert a string to uppercase; prints "HELLO"
print(s.rjust(7))      # Right-justify a string, padding with spaces
print(s.center(7))     # Center a string, padding with spaces
print(s.replace('l', '(ell)'))  # Replace all instances of one substring with another
print('  world '.strip())  # Strip leading and trailing whitespace

Hello
HELLO
hello
hello
he(ell)(ell)o
world

容器

  • Lists
    A list is the Python equivalent of an array, but is resizeable and can contain elements of different types:
xs = [3, 1, 2]   # Create a list
print(xs, xs[2])
print(xs[
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值