cheat-sheet-python-basics

1. Variables and Data Types

1.1 Variable Assignment

>>> x = 5
>>> x
    5

1.2 Calculations with Variables

>>> x + 2 # Sum of two variables
    7
>>> x - 2 # Subtraction of two variables
    3
>>> x * 2 # Multiplication of two variables
    10
>>> x ** 2 # Exponentiation of a variable
    23
>>> x % 2 # Remainder of a variable
    1
>>> x / float(2) # Division of a variable
    2.5

1.3 Types and Type Conversion

FunctionExampleExplanation
str ()‘5’, ‘3.45’, ‘True’Variables to strings
int()5, 3, 1Variables to integers
float()5.0, 1.0Variables to floats
bool()True, True, TrueVariables to booleans

2. Lists

>>> a = 'is'
>>> b = 'nice'
>>> my_list = ['my', 'list', a, b]
>>> my_list2 = [[4, 5, 6, 7],[3, 4, 5, 6]]

2.1 Selecting List Elements

  • Subset
>>> my_list[1] # Select item at index 1
>>> my_list[-3] # Select 3rd last item
  • Slice
>>> my_list[1:3] # Select items at index 1 and 2
>>> my_list[1: ] # Select items after index 0
>>> my_list[:3] # Select items before index 3
>>> my_list[:] # Copy my_list
  • Subset Lists of Lists
 >>> my_list2[1][0] # my_list[list][itemOfList]
 >>> my_list2[1][:2]

2.2 List Operations

>>> my_list + my_list
    ['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list * 2
    ['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list2 > 4
    True

2. 3 List Methods

FunctionExplanation
my_list.index (a)Get the index of an item
my_list.count (a)Count an item
my_list.append('!')Append an item at a time
my_list.remove('!')Remove an item
del(my_list[0:1])Remove an item
my_list.reverse()Reverse the list
my_list.extend('!')Append an item
my_list.pop(-1)Remove an item
my_list.insert(0,'!')Insert an item
my_list.sort()Sort the list

3. Strings

>>> my_string = 'thisStringIsAwesome'
>>> my_string
'thisStringIsAwesome'

3.1 String Operations

>>> my_string * 2
    'thisStringIsAwesomethisStringIsAwesome'
>>> my_string + 'Innit'
    'thisStringIsAwesomeInnit'
>>> 'm' in my_string
     True
>>> my_string[3] # index starts at 0
>>> my_string[4:9]

3.2 String Methods

FunctionExplanation
my_string.upper()String to uppercase
my_string.lower()String to lowercase
my_string.count('w')Count String elements
my_string.replace('e', 'i')Replace String elements
my_string.strip()Strip whitespaces

4. Numpy Arrays

>>> my_list = [1, 2, 3, 4]
>>> my_array = np.array(my_list)
>>> my_2darray = np.array([[1,2,3],[4,5,6]])

4.1 Selecting Numpy Array Elements

# Subset
>>> my_array[1] # Select item at index 1
    2 
# Slice
>>> my_array[0:2] # Select items at index 0 and 1
    array([1, 2])
# Subset 2D Numpy arrays
>>> my_2darray[:,0] # my_2darray[rows, columns]
    array([1, 4])

4.2 Numpy Array Operations

>>> my_array > 3
    array([False, False, False, True], dtype=bool)
>>> my_array * 2
    array([2, 4, 6, 8])
>>> my_array + np.array([5, 6, 7, 8])
    array([6, 8, 10, 12])

4.3 Numpy Array Functions

FunctionExplanation
my_array.shapeGet the dimensions of the array
np.append(other_array)Append items to an array
np.insert(my_array, 1, 5)Insert items in an array
np.delete(my_array, [1])Delete items in an array
np.mean(my_array)Mean of the array
np.median(my_array)Median of the array
my_array.corrcoef()Correlation coefficient
np.std(my_array)Standard deviation

5. Libraries

# Import libraries
>>> import numpy
>>> import numpy as np
# Selective import
>>> from math import pi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值