search
HomeDatabaseMysql Tutorial麻省理工公开课《计算机科学及编程导论》中文笔记(第2讲)

麻省理工公开课《计算机科学及编程导论》中文笔记(第2讲)作者 @易枭寒 知识点:运算符、运算对象,表达式,语句,分支、条件、循环 基本数据类型: 1、数字,数字用于数学运算。数字(整数,符点数,复数 complex numbers) 2、字符串,字符串是处理文字信

麻省理工公开课《计算机科学及编程导论》中文笔记(第2讲)作者@易枭寒


知识点:运算符、运算对象,表达式,语句,分支、条件、循环


基本数据类型:
1、数字,数字用于数学运算。数字(整数,符点数,复数 complex numbers)
2、字符串,字符串是处理文字信息的基本方式
3、布尔型(真、假)


表达式:运算 对象 运算符 运算对象,例如 1 + 1




>>> x = 3 #创建变量x,并为x赋值为3
>>> x = x*x #将结果9赋值给x
>>> print x
9




字符串复制操作:
>>> 'hello' * 3
'hellohellohello'
>>>




类型转换:
>>> 3 + a


执行后报错:
Traceback (most recent call last):
 File "", line 1, in
   3 + a
NameError: name 'a' is not defined


分析:语法上没有错误,运算对象 运算符 运算对象,但语义上有错误。
执行时,Python进行了类型检查,在运行程序前检测到了错误的运算对象类型。


类型转换与字符串的拼接操作:
>>> '3' + 'a'
'3a'
>>> str(3) + 'a'  #str(3):把数字3转换为字符型
'3a'


>>> 'a' False






运算符的优先级:类似数学运算里的先括号里的运算,再乘除,最后加减。
>>>3 + 4 * 5  # 表达式
23
>>>3 + (4 * 5) # 强烈建议加括号
23




赋值操作(绑定)、指针(连接):请自行画图,方便理解
>>>x = 3  #x存在于内存中某个地方,然后创建一个变量名与值之间的连接或指针
>>>z= x  #将y与同一个值绑定。这相当于将x的值或这个连接赋予z,结果z的指针指向相同位置,指向值,而不是x
>>>z
3


动态绑定:
变量的类型,你赋值为什么类型,它就是什么类型
>>>a = 3  # a为整型
>>>a = 'hello'  # a现在为字符串类型    
良好的编程风格:不要随意改变变量类型






statament (语句、声明):语句是做某事(换句话说就是,告诉计算机做什么),而表达式是某事。
        赋值语句是绑定变量名和值。print语句是输出到屏幕。
>>>2 * 2
4
>>>print 2 * 2
4
注意:在Python 3.0 中 print 是函数,即在Python3.0 中应该这样写 print(2*2)




注释:# 右边的信息为注释。注释写给读代码的人看得。机器不执行注释这行代码。


良好的编程风格:
1、写必要的、有意义的注释。
2、变量名的选取要有意义,name, age 见名之意,a,  b, x, y 没有意义,变量名要开头
    小写,单词之间用下划线连接,命名规则请自行搜索“变量命名 驼峰原则”。
3、不要反复无定的改变变量的值。不要随意改变变量类型。




保留字(关键字):Python中至少有28个保留字,也就是说它们已经被占用,变量命名的时候不
                             能用这些保留字了。


分支程序(Brouching programs)
条件执行和 if 语句:
注:以下代码需要下载Notepad++。因为在shell中缩进不明显。
请Google自行搜索 Notepad ++ python 下载配置(Tab转换为4个空格,快捷键运行)
将以下代码保存为if_else_odd.py文件。


#filename为if_else_odd.py
#判断奇数偶数
x = 15
if (x/2)*2 == x:
   print 'enen'
else:
   print 'odd'




如果 条件满足:
   执行print语句
否则:
   执行print 'odd'
注意:print语句前的4个空格缩进。它的意思是,该行代码是一个语言块。注意不要丢冒号。


#compare.py
#比较三个数的大小:
x = 15
y = 13
z = 11
print x, y, z


if x    print 'x is the smallest'
elif y    print 'y is the smallest'
else:
   print 'z is the smallest'


while循环:
#while.py
x = 10
i = 1
while (i    if x % i ==0:
       print 'yueshu', i
       i = i + 1
for循环:
#for.py


x = 10
for i in range(1, x):
   if x % i == 0:
       print 'yushu', i






Boolean(true ,false)  and or not


iteration迭代或loop循环
while


无限循环。

以上文字由  @易枭寒 (yixiaohan121318@gmail.com    QQ:499065469



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor