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

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

Jun 07, 2016 pm 03:25 PM
Chinese notes

麻省理工公开课《计算机科学及编程导论》中文笔记(第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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Can deleted notes on Xiaohongshu be recovered? Can deleted notes on Xiaohongshu be recovered? Oct 31, 2023 pm 05:36 PM

Notes deleted from Xiaohongshu cannot be recovered. As a knowledge sharing and shopping platform, Xiaohongshu provides users with the function of recording notes and collecting useful information. According to Xiaohongshu’s official statement, deleted notes cannot be recovered. The Xiaohongshu platform does not provide a dedicated note recovery function. This means that once a note is deleted in Xiaohongshu, whether it is accidentally deleted or for other reasons, it is generally impossible to retrieve the deleted content from the platform. If you encounter special circumstances, you can try to contact Xiaohongshu’s customer service team to see if they can help solve the problem.

How to set Chinese in Call of Duty: Warzone mobile game How to set Chinese in Call of Duty: Warzone mobile game Mar 22, 2024 am 08:41 AM

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Setting up Chinese with VSCode: The Complete Guide Setting up Chinese with VSCode: The Complete Guide Mar 25, 2024 am 11:18 AM

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

How to set Excel table to display Chinese? Excel switching Chinese operation tutorial How to set Excel table to display Chinese? Excel switching Chinese operation tutorial Mar 14, 2024 pm 03:28 PM

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

How to display Chinese characters correctly in PHP Dompdf How to display Chinese characters correctly in PHP Dompdf Mar 05, 2024 pm 01:03 PM

How to display Chinese characters correctly in PHPDompdf When using PHPDompdf to generate PDF files, it is a common challenge to encounter the problem of garbled Chinese characters. This is because the font library used by Dompdf by default does not contain Chinese character sets. In order to display Chinese characters correctly, we need to manually set the font of Dompdf and make sure to select a font that supports Chinese characters. Here are some specific steps and code examples to solve this problem: Step 1: Download the Chinese font file First, we need

An effective way to fix Chinese garbled characters in PHP Dompdf An effective way to fix Chinese garbled characters in PHP Dompdf Mar 05, 2024 pm 04:45 PM

Title: An effective way to repair Chinese garbled characters in PHPDompdf. When using PHPDompdf to generate PDF documents, garbled Chinese characters are a common problem. This problem usually stems from the fact that Dompdf does not support Chinese character sets by default, resulting in Chinese content not being displayed correctly. In order to solve this problem, we need to take some effective ways to fix the Chinese garbled problem of PHPDompdf. 1. Use custom font files. An effective way to solve the problem of Chinese garbled characters in Dompdf is to use

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

See all articles