腹痛腹泻吃什么药| 什么邮箱最好用最安全| 淋巴结肿大是什么引起的| 腰酸背痛是什么原因| 结婚25年属于什么婚| 忘恩负义的负是什么意思| lofter是什么意思| 不锈钢肥皂是什么原理| 朵的第二笔是什么| 甲状腺结节吃什么药| 起夜是什么意思| 什么是盆腔积液| 净身是什么| 避孕套是什么| 金戊念什么| 什么可以误诊为畸胎瘤| 怀孕前壁和后壁有什么区别| 富勒烯是什么| 吃人肉会得什么病| 洋字五行属什么| 红薯用什么繁殖| 科举制什么时候废除| 冠状动脉钙化什么意思| 梦见古墓是什么意思| 冬至穿什么衣服| 流鼻涕吃什么药好| 八面玲珑什么意思| 头发粗硬是什么原因| 甲沟炎去医院挂什么科| 保险凭证号是什么| 呻吟是什么意思| 月经来吃什么好| 牛肉排酸是什么意思| 综合用地是什么性质| 血糖高对身体有什么危害| 117是什么意思| 忘不了鱼在中国叫什么| 口气臭吃什么能改善| 第二天叫什么日| 心语是什么意思| 送什么礼物给女老师| 软绵绵的什么| 湿疹什么原因引起的| 月经颜色暗红色是什么原因| 脑供血不足有什么症状| 梦见挖土豆是什么意思| 风湿关节炎吃什么药| 古筝是什么乐器| 干咳无痰吃什么药好| 什么水果是降火的| est.是什么意思| 曲马多是什么药| 观落阴是什么意思| 移徒是什么意思| 天官是什么意思| 草木皆兵是什么生肖| 式可以加什么偏旁| 经常流鼻血是什么原因| 罪对什么| 来月经为什么会腰疼| 鸭锁骨是什么部位| 谝是什么意思| 让心归零是什么意思| tcl什么牌子| 希特勒为什么杀犹太人| 就诊卡号是什么| 一什么陆地| 什么叫辅酶q10| 眼底检查主要查什么| 重庆的市花是什么| 紫外线过敏什么症状| 嘴唇起皮是什么原因| 氧分压低是什么原因| 今年贵庚是什么意思| 上善若水是什么意思| 果葡糖浆是什么| 什么云见日| 彩超无回声是什么意思| 中药学是干什么的| 囊肿是什么意思| 文胸36码是什么尺寸| 刚愎自用什么意思| 女人梦见自己掉牙齿是什么征兆| 代谢慢是什么原因引起的| 猎德有什么好玩的| 集锦是什么意思| 举不胜举的举是什么意思| 茉莉什么时候开花| 肌钙蛋白高是什么意思| 胃底腺息肉什么意思| 月亮什么颜色| 月经过后腰酸疼是什么原因| 一笑泯恩仇什么意思| 4月29号0点是什么时候| 洋人是什么意思| 三叉神经痛吃什么药效果最好| 什么的身子| 脚上有水泡是什么原因| 怀孕了胃不舒服是什么原因| 何妨是什么意思| 什么面朝天| 乌鸦长什么样| 去湿气吃什么药| 椰浆和椰汁有什么区别| 肺结核是什么原因引起的| 全身痒但是身上什么都没有| 沙眼衣原体是什么意思| 走路带风是什么意思| 后背疼吃什么药| 2012年是什么年| 一线炎是什么病严重吗| 3n是什么意思| 笑口常开是什么生肖| 7777什么意思| 消化不良吃什么药最好| 背上长痘痘是什么原因| 看是什么意思| 舌苔厚白应该吃什么| 无什么什么力| 吃什么能胖起来| 北京有什么特产| 杆菌一个加号什么意思| 细菌性感染吃什么药| 半夜鸡叫是什么征兆| 感恩节为什么要吃火鸡| 防蓝光眼镜有什么用| 三句半是什么意思| 胃痛怎么办吃什么药| 肩周炎吃什么药效果最好| 运动喝什么水补充能量| 舌苔白腻吃什么药| 壁虎在家里是什么征兆| 心主什么| 抗核抗体阳性对怀孕有什么影响| 什么叫贵妃镯| 抑郁症发作是什么感觉| 漏是什么意思| 男生生日礼物送什么| 减肥为什么让早上空腹喝咖啡| 冢字的意思是什么| 碧玺五行属什么| 灼热感是什么样的感觉| 子宫肌瘤是什么原因导致的| 阿拉伯人属于什么人种| rian是什么意思| Iud是什么| up主是什么意思| 无公害什么意思| 计划生育是什么意思| 喉咙有浓痰是什么原因| fans是什么意思| 女性白带发黄是什么原因| 饱和什么意思| 女人为什么比男人长寿| 90年出生属什么生肖| 麝香是什么味道| 什么时候验孕最准确| 为什么胃有灼热感| 心什么如什么的成语| 囗苦是什么原因| baumwolle是什么面料| 栅栏是什么意思| 俺是什么意思| 尿路感染什么症状| 警示是什么意思| 蟋蟀吃什么| 三顾茅庐的顾是什么意思| 腺癌是什么原因引起的| 什么原因导致胎停| 煤气罐在什么情况下会爆炸| 80年属猴的是什么命| 酒酿蛋什么时候吃效果最好| 马住什么意思| 2月18日是什么星座| 前列腺吃什么药好| 早醒是什么原因| 中秋节的习俗是什么| 6月26日是什么星座| 红细胞偏高有什么危害| 屠苏酒是什么酒| lxy是什么意思| 谷草转氨酶偏低是什么原因| 2157是什么意思| 阴唇是什么颜色| 头发偏黄是什么原因| 奥美拉唑治什么胃病| hpf医学是什么意思| 西红柿有什么营养| 山及念什么| 打喷嚏流鼻涕吃什么药| 仓鼠能吃什么水果| 液氮是什么| 孜孜不倦什么意思| 端午节喝什么酒| 口干口苦吃什么中成药| 国家能源局是什么级别| 前哨是什么意思| 花胶是什么| 大便溏泄是什么意思| 囤货是什么意思| 番茄和蕃茄有什么区别| 自燃是什么意思| 一个令一个瓦念什么| 女朋友生日送什么礼物| 口加至念什么| 什么样人不能吃海参| 吃什么减肚子上的赘肉最快| 甲申日五行属什么| wh是什么颜色| 婴儿胎发什么时候剪最好| 闭合性跌打损伤是什么意思| 甲状腺1类是什么意思| 仓鼠能吃什么东西| 一九六三年属什么生肖| 倒数是什么意思| 尿少是什么原因| 尿液清澈透明说明什么| 馥字五行属什么| 社畜什么意思| bpc是什么意思| 做梦梦见狗咬我什么意思啊| 咽炎吃什么药好使| 什么是散光| 大便为什么是黑色的是什么原因| 黑眼圈挂什么科| 过敏去医院挂什么科| 解说是什么意思| 5.23是什么星座| 梦见下大雨是什么意思| 拉肚子是什么原因引起的| 南瓜子不能和什么一起吃| 上颌窦囊肿是什么意思| 48岁属什么| 为什么不建议开眼角| 公务员五行属什么| 什么叫近视| 瘦马什么意思| 视线模糊是什么原因| 疱疹是什么样的| 心肌缺血有什么症状和表现| hpv感染是什么意思| 头发为什么会变白| 大便拉不出来是什么原因| 丹参有什么作用和功效| 复试是什么意思| 三叉神经痛挂什么科就诊| 葡萄糖有什么作用| 补气固表什么意思| 腐女是什么| 幽门螺杆菌是什么| 孩子记忆力差是什么原因| 什么是占有欲| 内膜薄是什么意思| 荔枝什么品种好吃| 鹅蛋脸适合什么样的发型| 什么人不能喝牛奶| 以色列人说什么语言| 骨性关节炎吃什么药| 贵字五行属什么| 多囊吃什么药| 为什么会得心脏病| 12月21日什么星座| 何妨是什么意思| 西游记什么时候写的| 百度Jump to content

手机厂商开启“全面屏”竞争 下半年逐渐量产

From Wikibooks, open books for an open world
百度 科考队在南极作业期间开展应急演练,就是要使全体队员面临火灾和被迫弃船等危机时临危不乱,果断处置,确保队员生命和财产安全。


In this section, you will be introduced to two different kinds of data in Python: variables and strings. Please follow along by running the included programs and examining their output.

Variables

[edit | edit source]

A variable is something that holds a value that may change. In simplest terms, a variable is just a box that you can put stuff in. You can use variables to store all kinds of stuff, but for now, we are just going to look at storing numbers in variables.

lucky = 7
print (lucky)
7

This code creates a variable called lucky, and assigns to it the integer number 7. When we ask Python to tell us what is stored in the variable lucky, it returns that number again.

We can also change what is inside a variable. For example:

 

changing = 3                                   
print (changing)
3 

changing = 9
print (changing)
9

different = 12
print (different)
12
print (changing)
9

changing = 15
print (changing)
15

We declare a variable called changing, put the integer 3 in it, and verify that the assignment was properly done. Then, we assign the integer 9 to changing, and ask again what is stored in changing. Python has thrown away the 3, and has replaced it with 9. Next, we create a second variable, which we call different, and put 12 in it. Now we have two independent variables, different and changing, that hold different information, i.e., assigning a new value to one of them is not affecting the other.

You can also assign the value of a variable to be the value of another variable. For example:

red = 5
blue = 10
print (red, blue)
5 10

yellow = red
print (yellow, red, blue)
5 5 10

red = blue
print (yellow, red, blue)
5 10 10

To understand this code, keep in mind that the name of the variable is always on the left side of the equals sign (the assignment operator), and the value of the variable is on the right side of the equals sign. First the name, then the value.

We start out declaring that red is 5, and blue is 10. As you can see, you can pass several arguments to print to tell it to print multiple items on one line, separating them by spaces. As expected, Python reports that red stores 5, and blue holds 10.

Now we create a third variable, called yellow. To set its value, we tell Python that we want yellow to be whatever red is. (Remember: name to the left, value to the right.) Python knows that red is 5, so it also sets yellow to be 5.

Now we're going to take the red variable, and set it to the value of the blue variable. Don't get confused — name on the left, value on the right. Python looks up the value of blue, and finds that it is 10. So, Python throws away red's old value (5), and replaces it with 10. After this assignment Python reports that yellow is 5, red is 10, and blue is 10.

But didn't we say that yellow should be whatever value red is? The reason that yellow is still 5 when red is 10, is because we only said that yellow should be whatever red is at the moment of the assignment. After Python has figured out what red is and assigned that value to yellow, yellow doesn't care about red any more. yellow has a value now, and that value is going to stay the same no matter what happens to red.

Note: The interplay between different variables in Python is, in fact, more complex than explained here. The above example works with integer numbers and with all other basic data types built into Python; the behavior of lists and dictionaries (you will encounter these complex data types later) is entirely different, though. You may read the chapter on data types for a more detailed explanation of what variables really are in Python and how their type affects their behavior. However, it is probably sufficient for now if you just keep this in mind: whenever you are declaring variables or changing their values, you always write the name of the variable on the left of the equals sign (the assignment operator), and the value you wish to assign to it on the right.

For the name of the variable, it can only consist of uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore character (_), and the first character of the name cannot be a digit. For example, 1abc and _#$ad are not valid variable names, while _123 and a__bc are valid variable names.

String

[edit | edit source]

A 'string' is simply a list of characters in order. A character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. For example, "hello" is a string. It is five characters long — h, e, l, l, o. Strings can also have spaces: "hello world" contains 11 characters: 10 letters and the space between "hello" and "world". There are no limits to the number of characters you can have in a string — you can have anywhere from one to a million or more. You can even have a string that has 0 characters, which is usually called an "empty string."

There are three ways you can declare a string in Python: single quotes ('), double quotes ("), and triple quotes ("""). In all cases, you start and end the string with your chosen string declaration. For example:

>>> print ('I am a single quoted string')
I am a single quoted string
>>> print ("I am a double quoted string")
I am a double quoted string
>>> print ("""I am a triple quoted string""")
I am a triple quoted string

You can use quotation marks within strings by placing a backslash directly before them, so that Python knows you want to include the quotation marks in the string, instead of ending the string there. Placing a backslash directly before another symbol like this is known as escaping the symbol.

>>> print ("So I said, \"You don't know me! You'll never understand me!\"")
So I said, "You don't know me! You'll never understand me!"
>>> print ('So I said, "You don\'t know me! You\'ll never understand me!"')
So I said, "You don't know me! You'll never understand me!"
>>> print ("""The double quotation mark (\") is used to indicate direct quotations.""")
The double quotation mark (") is used to indicate direct quotations.

If you want to include a backslash in a string, you have to escape said backslash. This tells Python that you want to include the backslash in the string, instead of using it as an escape character. For example:

>>> print ("This will result in only three backslashes: \\ \\ \\")
This will result in only three backslashes: \ \ \

As you can see from the above examples, only the specific character used to quote the string needs to be escaped. This makes for more readable code.

To see how to use strings, let's go back for a moment to an old, familiar program:

>>> print("Hello, world!")
Hello, world!

Look at that! You've been using strings since the very beginning!

You can add two strings together using the + operator: this is called concatenating them.

>>> print ("Hello, " + "world!")
Hello, world!

Notice that there is a space at the end of the first string. If you don't put that in, the two words will run together, and you'll end up with Hello,world!

You can also repeat strings by using the * operator, like so:

>>> print ("bouncy " * 5)
bouncy bouncy bouncy bouncy bouncy 
>>> print ("bouncy " * 10)
bouncy bouncy bouncy bouncy bouncy bouncy bouncy bouncy bouncy bouncy

The string bouncy gets repeated 5 times in the 1st example and 10 times in the 2nd.

If you want to find out how long a string is, you use the len() function, which simply takes a string and counts the number of characters in it. (len stands for "length.") Just put the string that you want to find the length of inside the parentheses of the function. For example:

>>> print (len("Hello, world!"))
13

Strings and Variables

[edit | edit source]

Now that you've learned about variables and strings separately, let's see how they work together.

Variables can store much more than just numbers. You can also use them to store strings! Here's how:

question = "What did you have for lunch?"
print (question)
What did you have for lunch?

In this program, we are creating a variable called question, and storing the string "What did you have for lunch?" in it. Then, we just tell Python to print out whatever is inside the question variable. Notice that when we tell Python to print out question, there are no quotation marks around the word question: this tells Python that we are using a variable, not a string. If we put in quotation marks around question, Python would treat it as a string, as shown below:

question = "What did you have for lunch?"
print ("question")
question

Let's try something different. Sure, it's all fine and dandy to ask the user what they had for lunch, but it doesn't make much difference if they can't respond! Let's edit this program so that the user can type in what they ate.

question = "What did you have for lunch?"
print (question)
answer = raw_input() #You should use "input()" in python 3.x, because python 3.x doesn't have a function named "raw_input".

print ("You had " + answer + "! That sounds delicious!")

To ask the user to write something, we used a function called raw_input(), which waits until the user writes something and presses enter, and then returns what the user wrote. Don't forget the parentheses! Even though there's nothing inside of them, they're still important, and Python will give you an error if you don't put them in. You can also use a different function called input(), which works in nearly the same way. We will learn the differences between these two functions later.

Note: In Python 3.x raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it without the trailing newline. It raises EOFError if the input is terminated prematurely (e.g. by pressing Ctrl+D). To get the old behavior of input(), use eval(input()).

In this program, we created a variable called answer, and put whatever the user wrote into it. Then, we print out a new string, which contains whatever the user wrote. Notice the extra space at the end of the "You had " string, and the exclamation mark at the start of the "! That sounds delicious!" string. They help format the output and make it look nice, so that the strings don't all run together.

Combining Numbers and Strings

[edit | edit source]

Take a look at this program, and see if you can figure out what it's supposed to do.

print ("Please give me a number: ")
number = raw_input()

plusTen = number + 10
print ("If we add 10 to your number, we get " + plusTen)

This program should take a number from the user, add 10 to it, and print out the result. But if you try running it, it won't work! You'll get an error that looks like this:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print "If we add 10 to your number, we get " + plusTen
TypeError: cannot concatenate 'str' and 'int' objects

What's going on here? Python is telling us that there is a TypeError, which means there is a problem with the types of information being used. Specifically, Python can't figure out how to reconcile the two types of data that are being used simultaneously: integers and strings. For example, Python thinks that the number variable is holding a string, instead of a number. If the user enters 15, then number will contain a string that is two characters long: a 1, followed by a 5. So how can we tell Python that 15 should be a number, instead of a string?

Also, when printing out the answer, we are telling Python to concatenate together a string ("If we add 10 to your number, we get ") and a number (plusTen). Python doesn't know how to do that -- it can only concatenate strings together. How do we tell Python to treat a number as a string, so that we can print it out with another string?

Luckily, there are two functions that are perfect solutions for these problems. The int() function will take a string and turn it into an integer, while the str() function will take an integer and turn it into a string. In both cases, we put what we want to change inside the parentheses. Therefore, our modified program will look like this:

print ("Please give me a number:",)
response = raw_input()

number = int(response) 
plusTen = number + 10

print ("If we add 10 to your number, we get " + str(plusTen))

Note: Another way of doing the same is to add a comma after the string part and then the number variable, like this:

print ("If we add 10 to your number, we get ", plusTen)

or use special print formatting like this:

print ("If we add 10 to your number, we get %s" % plusTen)

which alternative can be written this way, if you have multiple inputs:

plusTwenty = number + 20
print ("If we add 10 and 20 to your number, we get %s and %s" % (plusTen, plusTwenty))

or use format()

print ("If we add 10 to your number, we get {0}".format(plusTen))

That's all you need to know about strings and variables! We'll learn more about types later.

List of Learned Functions

[edit | edit source]
  • print(): Print the output information to the user
  • input() or raw_input(): asks the user for a response, and returns that response. (Note that in version 3.x raw_input() does not exist and has been replaced by input())
  • len(): returns the length of a string (number of characters)
  • str(): returns the string representation of an object
  • int(): given a string or number, returns an integer

Note: #input and raw_input function accept a string as parameter. This string will be displayed on the prompt while waiting for the user input.

  1. The difference between the two is that raw_input accepts the data coming from the input device as a raw string, while input accepts the data and evaluates it into python code. This is why using input as a way to get a user string value returns an error because the user needs to enter strings with quotes.

It is recommended to use raw_input at all times and use the int function to convert the raw string into an integer. This way we do not have to bother with error messages until the error handling chapter and will not make a security vulnerability in your code.

Exercises

[edit | edit source]
  1. Write a program that asks the user to type in a string, and then tells the user how long that string was.
  2. Ask the user for a string, and then for a number. Print out that string, that many times. (For example, if the string is hello and the number is 3 you should print out hello hello hello .)
  3. What would happen if a mischievous user typed in a word when you ask for a number? Try it.

Solutions

Quiz

文火是什么火 婴儿补铁吃什么铁剂 头部ct能检查出什么 小便赤黄是什么原因 腐是什么意思
银壶一般什么价位 柔顺和拉直有什么区别 甲亢做什么检查 sorona是什么面料 黑色裤子配什么颜色t恤
濒危是什么意思 大象鼻子为什么那么长 dove什么意思 肩胛骨疼挂什么科 超体2什么时候上映
指甲盖发紫是什么原因 改姓氏需要什么手续 荷叶有什么功效和作用 梦见掉头发是什么意思 为什么会梦到一个人
补睾丸吃什么药最好hcv8jop8ns4r.cn 桑叶长什么样子图片hcv9jop5ns2r.cn 多吃火龙果有什么好处和坏处hcv9jop4ns6r.cn 刚出生的小鱼吃什么youbangsi.com 餐标是什么意思kuyehao.com
ins风格是什么hcv8jop0ns9r.cn 保鲜卡是什么原理纸片hcv9jop3ns9r.cn 真维斯属于什么档次hcv8jop2ns7r.cn 梦见被雨淋是什么意思hcv9jop5ns9r.cn 星期天左眼皮跳是什么预兆hcv8jop7ns7r.cn
牛油果有什么功效onlinewuye.com 前列腺增大伴钙化是什么意思hcv8jop9ns3r.cn plano是什么意思yanzhenzixun.com 小孩流鼻涕咳嗽吃什么药tiangongnft.com 小王子讲了什么故事hcv9jop4ns2r.cn
腰疼是什么病hcv9jop6ns5r.cn pcv是什么意思hcv8jop7ns6r.cn 咳嗽白痰吃什么药wmyky.com 揣测是什么意思hcv8jop2ns7r.cn 被虫子咬了涂什么药膏youbangsi.com
百度