首页 > 编程语言 >Review-python-Note1

Review-python-Note1

时间:2022-10-06 20:15:19浏览次数:52  
标签:index python Review list element Note1 print True operators

Note 1

标签:python

目录


learning techniques

take python as a calculator and build the whole architecture from this base gradually.

1.format

1.1 comment

### single line comment
''' ''' multiple lines comment 1
""" """ multiple lines comment 2

1.2 line and indence

Different from Java and CPP does,python won't use curly brace but indence instead.The adjoining code blocks with the same priority should have same indence.

if a > 1 :
    b = 1
    c = 2

2.number

2.1 how many types of number does a methematical calculator may have?

(Python accepts these types of input.But you should understand how these numbers are stored into device.)

  1. integer
  2. float number
  3. fraction
  4. complex number(real number + imaingary number)
  5. bool

I: Integer is categorized into int and long int labelled by L most time and its lower case 'l' hardly \(\to\):eschewing the ambiguity between l and 1(:number 1).

II: boolean variables and some similar functions:type(),isinstance(),issubclass()

# 1 acts as boolean variable -> True
if 1 :
    print ("1 can also be \"True\" ")
else :
    print ("1 cannot also be \"True\" ")

# True can add False as well as numbers 
print(True + False + 0)

#function type()
bool1 = (1==1)
print(type(bool1))

#Given B extends A
#type(B) won't produce A
#but isinstance(B,A) produces True
print(isinstance(bool1,int))


#judge whethere parameter 1 is the child class of parameter 2
print(issubclass(bool,int))


III: complex(real number,imaginary number):

#produce 12 + 34j
print(complex(12,34))

3.string

(stretch from number to char and string initially with an assumption of only a calculator )

3.1 representation

enclosed by single quote '' or double quote ""

Example: 'hello' "world!"

3.2 index

Given a = "python"

start from the beginning with the index 0 and step forward with one step increased:

0 -> length-1 : a[0] == 'p' and a[5] == 'n'

start from the end with the index -1 and step backward with one step decreased:

-1 - length - 1 -> -1 : a[-6] == 'p' and a[-1] == 'n'

Note:(ruminate this snippet)

a = "HelloWorld!"
a[3] = 'X'

Terminal shell prompts:

Traceback (most recent call last):

  File "c:\Users\ASUS\test.py", line 5, in <module>
  
    a[3] = 'X'
    
TypeError: 'str' object does not support item assignment

\(\to\)the re-assignment to one element in a string is forbidden.

3.3 segment(as dafny does)

Given a = "python"

#will produce "on" from position 4 to 5(position 6 is exclusive).
print(a[4:6])

#will produce "python" from the beginning to the end
print(a[:])
print(a[0:])
print(a[:6])

4.operator

a mathematical calculator,yet more often than not,has

  • addition
  • subtraction
  • muliplcation
  • power
  • division
  • model(取模?单词好像不对?)

these 6 kinds of operation.

So without further ado,let's shed light on what kinds of operation python supports?

4.1 arithmetic operators

4.1.1 addition

Example:

#produce 2 
print(1+1)

4.1.2 subtraction

Example:

#produce 1
print(2-1)

4.1.3 multiplication

Example:

#produce 4
print(2*2)

4.1.4 power

Example:

#produce 9
print(3**2)

4.1.5 division and round down

Example:

#produce 0.3333333333333333(but i guess that the different CPU produce different outcomes)
print(1/3)
#produce 0.5
print(1/2)
#round down ->produce 1
print(3/2)

4.1.6 modulo operation

Example:

#produces 1
# 7 / 3 = 2...1
print(7%3)

4.2 comparsion operators

The outcome of comparsion operators is always bool.

operators explanation
== 3==1 ==> False ; 1==1= => True;
> 1>0 ==> True ; 0>1 ==> False;
< 1<0 ==>False ; 0<1 ==> True;
>= 1>=1 ==> True ; 0>=1 ==> False;
<= 1<=1 ==> True ; 2<=1 ==> False;

4.3 assignment operators

operators explanation
= a=1 : assign 1 to a
+= a+=1 : a=a+1
-+ a-=1 : a=a-1
*= a=1 : a=a1
/= a/=1 : a=a/1
%/ a%=1 : a=a%1
**= a=1;a=a1
:= assign number to variable in expression

4.4 logic operators

operators explanation
and equivalent to &&
or equivalent to ||
not equivalent to !

4.5 Identity operators

Note: Not simply equal,but if these two object are the same object ,literally with the same memory location.

operators explanation
is return true if same object
is not return true if different object

4.6 Membership operators

Notes: usually use in list,tuple,set,dictionary

operators explanation
in return true if a sequence has this element
not in return true if a sequence doesn't have this element

4.7 Bitewise operators????

5 Sequence

5.1 Introduction to sequence

  1. sequence is the most basic data structure in python.
  2. every element in sequence has its own index,begin with 0 and increases by order.
  3. There are six types of in-built sequences in python.

5.2 List

5.2.1 Definition

List is similar with array in Java,but it accepts several type in one list.

#declaration
a=[0,1,2,3]

5.2.2 index

the index starts from 0 and inceases by order.

Note:
There is another specific index system here:
the index start from the end of list and steps backwards decreasingly

5.2.3 seize List from a novel perspective : CRUD

1.Create
#declaration
a=[0,1,2,3]
2.Read

Index plays an important role in retrieving element here.
Literally we retrieves element with index to get element.

a[0] # a[0] = 0
3.Update
Genre Explanation
change the original element a[0] = 1 : change the element placed at index 0 to 1
append single element to the end of a list a.append('a') a.append("hello")
append another list to the end of a list [0,1,2,3]+[4,5,6,7]
duplicate list n times as a new list [0,1]*2 : [0,1,0,1]
slice(as dafny does) shows below separately

slice

[a:b] means cut from a to b but exclusive of b

a=[0,1,2,3,4,5]
print(a[:]) # [0,1,2,3,4,5]
print(a[0:]) # [0,1,2,3,4,5]
print(a[:1]) # [0]
print(a[0:1]) # [0]
4.delete

del() and listIdentifier.remove() both works

del a[5] # delete the element positioned at index 5
a.remove("hello") # remove first one matched element from the beginning

5.2.4 Nested list

a=[
    [2,2],
    [3,3], 
    [4,4],
    1
]
print a

#outcome is : [[2, 2], [3, 3], [4, 4], 'hello']

5.2.5 compare different lists

import operator

a=[11]
b=[11]
c=[22]
print(operator.eq(a,b))
print(operator.eq(a,c))
print(operator.eq(a,b))
# True False

5.2.6 several crucial relevant functions

manipulate the list

function description
len(list) return the length of a list
list(a) a -> tuple or string : change a to a list
min(a) return the minimum in a list
max(a) return the maximum in a list
list.reverse() reverse a list
list.clear() clear a list out
list.copy() duplicate a list

manipulate the element in a list

function description
list.append(element) element->any type : append to a list
list.count(element) count how many times this element occurs
list.expand(seq) seq->any kind of seq whatever tuper,list,set,dict : append this seq to the end of a list but if seq is dict then all keys are appended to the end
list.index(obj) return the index at which element matches obj
list.insert(index,obj) insert obj onto the position of list
list.pop(index(:=-1)) index->deault = -1 : pop up the last element

标签:index,python,Review,list,element,Note1,print,True,operators
From: https://www.cnblogs.com/UQ-44636346/p/16749448.html

相关文章

  • 对比python学julia(第四章:人工智能)--(第二节)人脸识别
    2.1. 项目简介人脸识别是基于人的脸部特征信息进行身份识别的一种图像识别技术。使用0PenCV进行人脸识别的过程如下。(1) 针对每个识别对象收集大量的......
  • python中的矩阵乘法
    1.np.multiply()函数 矩阵的对应位置相乘,如果其中一个矩阵的尺寸不够,会自动广播,但是尺寸不能广播就会报错2.np.dot()函数 矩阵的点积,又称数量积、标量积或内积,即一......
  • 分享13个非常有用的python代码片段
    分享13个非常有用的python代码片段listssnippets我们先从最常用的数据结构列表开始1\将两个列表合并成一个字典假设我们在python中有两个列表,我们希望将它们合并成为......
  • python session手动添加cookies键值并保持
    importrequestsses=requests.session()requests.utils.add_dict_to_cookiejar(ses.cookies,{"sessionid":"04r6wd81ew8egds5e8d16fe8g45s"})headers={'user-agen......
  • DeepMind Lab的一些python例子—————(Ubuntu22.04系统安装DeepMind Lab)后续
    相关资料:Ubuntu22.04系统安装DeepMindLab ======================================================  importdeepmind_labimportnumpyasnp#Createane......
  • python 列表
    1.基本语法#字面量[元素1,元素2,元素3,元素4,...]#定义变量变量名字=[元素1,元素2,元素3,元素4,...]#定义空列表变量名字=[]变量名字=list()列表内的每个数据,......
  • python 创建虚拟环境
    1、安装virtualenv库pipinstallvirtualenv2、创建虚拟环境virtualenv[虚拟环境名称]virtualenvvenv#如果不想使用系统的包,加上–no-site-packeages参数v......
  • python 数据容器
    python中的数据容器一种可以容纳多份数据的数据类型,容纳的每一份数据称之为一个元素,每个元素,可以是任意类型的数据,如字符串,数字,布尔等。数据容器更具特点不同,如:是否支......
  • python
    1.计算机基础知识2.python基础3.流程控制4.数据类型内置方法5.文件操作......
  • 【python】采集可爱猫咪数据并作可视化
    前言嗨喽~大家好呀,这里是魔王呐!环境介绍:python3.6pycharm爬虫部分使用模块:csvrequests>>>pipinstallrequestsparsel如何安装python第三方......