{"id":2419,"date":"2025-09-02T15:31:54","date_gmt":"2025-09-02T07:31:54","guid":{"rendered":"http:\/\/blue.yn.cn\/?p=2419"},"modified":"2025-11-01T21:43:38","modified_gmt":"2025-11-01T13:43:38","slug":"veriables","status":"publish","type":"post","link":"http:\/\/blue.yn.cn\/?p=2419","title":{"rendered":"2. Veriables"},"content":{"rendered":"<p><strong>Variables, expressions, and statements<\/strong><\/p>\n<h3>values and types<\/h3>\n<p>A value is one of the basic thing a program works with, like a letter or a number. The values we have seen so far are 1,2, and &quot;hello world!&quot;<\/p>\n<p>These values belong to different types: 2 is an integer, and &quot;hello world!&quot; is a string, so called because is contains a &quot;string&quot; of letters. You (and the interpreter)can identify strings because they are enclosed in quotation marks.<\/p>\n<p>The <code>print<\/code> statements also works for integer. We use the <code>python<\/code> command to start the interpreter.<\/p>\n<pre><code class=\"language-python\">python\n>&gt;&gt; print(4)\n4<\/code><\/pre>\n<p>If you are not sure what type a value has, the interpreter can tell you.<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; type(&#039;Hello,world!&#039;)\n&lt;class &#039;str&#039;&gt;\n>&gt;&gt; type(17)\n&lt;class &#039;int&#039;&gt;<\/code><\/pre>\n<p>Not surprisingly,strings belong to the type <code>str<\/code> and integers belong to the type <code>int<\/code>. Less obviously, numbers with a decimal point belong to a type called <code>float<\/code>, because these numbers are represented in a format called floating point.<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; type(3.2)\n&lt;class &#039;float&#039;&gt;<\/code><\/pre>\n<p>What about values like &quot;17&quot; and &quot;3.2&quot;? They look like numbers, but they are in quotation marks like strings.<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; type(&#039;17&#039;)\n&lt;class &#039;str&#039;&gt;\n>&gt;&gt; type(&#039;3.2&#039;)\n&lt;class &#039;str&#039;&gt;<\/code><\/pre>\n<p>They're strings.<\/p>\n<p>When you type a large integer, you might be tempted to use commas between groups of three digits, as in 1,000,000. This is not a legal integer in Python, but it is legal:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; print(1,000,000)\n1 0 0<\/code><\/pre>\n<p>Well, that's not what we expected at all! Python interprets 1,000,000 as a comma-separated sequence of integers, which it prints with spaces between.<\/p>\n<p>This is the first example we have seen of a semantic error: the code runs without producing an error message, but it doesn't do the &quot;right&quot; thing.<\/p>\n<h3>Variables<\/h3>\n<p>One of the most powerful feature of a programming language is the ability to manipulate variable. A variable is a name refers to a value.<\/p>\n<p>An assignment statement creates new variables and gives them values:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; message = &#039;And now for something completely different&#039;\n>&gt;&gt; n = 17\n>&gt;&gt; pi = 3.1415926535897931<\/code><\/pre>\n<p>This example makes three assighments. The first assighs a string a new varialbe named <code>message<\/code>; the second assighs the integer 17 to <code>n<\/code>; the third assighs the(approximate)value of \u03c0 to <code>pi<\/code>.<\/p>\n<p>To display the vlaue of a variable, you can use a print statement:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; print(n)\n17\n>&gt;&gt; print(pi)\n3.141592653589793<\/code><\/pre>\n<p>The type of a variable is the type of the value it refers to.<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; type(message)\n&lt;class &#039;str&#039;&gt;\n>&gt;&gt; type(n)\n&lt;class &#039;int&#039;&gt;\n>&gt;&gt; type(pi)\n&lt;class &#039;float&#039;&gt;<\/code><\/pre>\n<h3>Variable names and keywords<\/h3>\n<p>Programmers generally choose names of their variables that are meaningful and document what the variable is used for.<\/p>\n<p>variable names can be arbitrarily long. They can contain both letters and numbers, but they cannot start with a number. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter(you'll see why later).<\/p>\n<p>The underscore charater(_) can appear in a name. It is often used in names with multiple words, such as <code>my_name<\/code> or <code>airspeed_of_unladen_swallow<\/code>.<br \/>\nVariable names can start with an underscore charater, but we generally aviod doing this unless we are writing library code for others to use.<\/p>\n<p>If you give a variable an illegal name, you get a syntax error:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; 76trombones = &#039;big parade&#039;\nSyntaxError: invalid syntax\n>&gt;&gt; more@ = 1000000\nSyntaxError: invalid syntax\n>&gt;&gt; class = &#039;Advanced Theoretical Zymurgy&#039;\nSyntaxError: invalid syntax<\/code><\/pre>\n<p><code>76trombones<\/code> is illegal because it begings with a number.<br \/>\n<code>more@<\/code> is illegal because it contains an illegal charater, @. But what's wrong with <code>class<\/code>?<\/p>\n<p>It turns out that <code>class<\/code> is one of Python's keywords. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names.<\/p>\n<p>Python reserves 35 keywords:<\/p>\n<pre><code class=\"language-python\">False      await      else       import     pass\nNone       break      except     in         raise\nTrue       class      finally    is         return\nand        continue   for        lambda     try\nas         def        from       nonlocal   while\nassert     del        global     not        with\nasync      elif       if         or         yield<\/code><\/pre>\n<p>You might want to keep this list handy. If the interpreter complains about one of your variable names and you don't know why, see if it is on this list.<\/p>\n<h3>Statements<\/h3>\n<p>A statement is a unit of code that the Python interpreter can execute. We have seen two kinds of staements: print being an expression assignment.<\/p>\n<p>When you type a statement in interactive mode, the interpreter executes it and displays the result, if there is one.<\/p>\n<p>A script usually contains a sequence of statement. If there is more than one statement, the rusults appear one at a time as the statements execute.<\/p>\n<p>For example, the script<\/p>\n<pre><code class=\"language-python\">print(1)\nx = 2\nprint(x)<\/code><\/pre>\n<p>produces the output<\/p>\n<pre><code class=\"language-python\">1\n2<\/code><\/pre>\n<p>The assignment statement produces no output.<\/p>\n<h3>Operators and operands<\/h3>\n<p>Operators are special symbols that represent computations like addition and multiplication. The values the operator is apllied to are called operands.<\/p>\n<p>The operators <code>+<\/code> <code>-<\/code> <code>*<\/code> <code>\/<\/code>, and <code>**<\/code> perform addition, subtraction, multiplication, division, and exponential\uff0c as in the following example:<\/p>\n<pre><code class=\"language-python\">20+32\nhour-1\nhour*60+minute\nminute\/60\n5**2\n(5+9)*(15-7)<\/code><\/pre>\n<p>There has been a change in the division operator between Python 2 and Python 3. In Python3, the result of this division is a floating point result:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; minute = 59\n>&gt;&gt; minute\/60\n0.9833333333333333<\/code><\/pre>\n<p>The division operator in Python 2 would divide two integers and truncate the result to an integer:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; minute = 59\n>&gt;&gt; minute\/60\n0<\/code><\/pre>\n<p>To obtain the same answer in Python 3 use floored(<code>\/\/<\/code> integer) division.<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; minute = 59\n>&gt;&gt; minute\/\/60\n0<\/code><\/pre>\n<p>In python 3 integer division functions much more as you would expect if you entered the expression on a calculation.<\/p>\n<h3>Expressions<\/h3>\n<p>An expression is a combination of values, variable, and operators.<br \/>\nA value all by itself is considered an expression, and so is a variable, so the following are all legal expression(assuming that the variable <code>x<\/code> has been assigned a value)<\/p>\n<pre><code class=\"language-python\">17\nx\nx + 17<\/code><\/pre>\n<p>If you type an expression in interactive mode, the interpreter evaluates it and displays the result:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; 1+1\n2<\/code><\/pre>\n<p>But in a script, an expression all by itself doesn't do anything! This is common source of confusion for beginners.<\/p>\n<p><strong>Exercise 1:<\/strong> Type the following statements in the Python interpreter to see what they do:<\/p>\n<pre><code class=\"language-python\">5\nx = 5\nx + 1<\/code><\/pre>\n<h3>Order of operations<\/h3>\n<p>When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence.<br \/>\nFor mathematical operators, Python follows mathematical convention.<br \/>\nThe acronym PEMDA is a useful way to remenber the rules:<\/p>\n<ul>\n<li>\n<p>Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluate first, <code>2 * (3-1)<\/code> is 4, and <code>(1+1)**(5-2)<\/code> is 8. You can also use parentheses to make an expression easier to read, as in <code>(minute * 100) \/ 60<\/code>, even if it doesn't change the result.<\/p>\n<\/li>\n<li>\n<p>Exponential has the next highest precedence, so <code>2**1+1<\/code> is 3, not 4, <code>3*1**3<\/code> is 3, not 27.<\/p>\n<\/li>\n<li>\n<p>Multiplication and division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So <code>2*3-1<\/code> is 5, not 4, and <code>6+4\/2<\/code> is 8, not 5.<\/p>\n<\/li>\n<li>\n<p>Operators with the same precedence are evaluated from left to right. So the expression <code>5-3-1<\/code> is 1, not 3, because the <code>5-3<\/code> happens first and then <code>1<\/code> is subtracted from 2.<\/p>\n<\/li>\n<\/ul>\n<p>When in doubt, always put parentheses in your expressions to make sure the computations are performed in the order you intend.<\/p>\n<h3>Modulus operator<\/h3>\n<p>The modulus operator works on integers and yields the remainder when the first operand is divided by the second. In Pyton, the modulus operator is a percent sign(<code>%<\/code>). The syntax is the same as for other operators:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; quotient = 7 \/\/ 3\n>&gt;&gt; print(quotient)\n2\n>&gt;&gt; remainder = 7 % 3\n>&gt;&gt; print(remainder)\n1<\/code><\/pre>\n<p>So 7 divided by 3 is 2 with 1 left over.<\/p>\n<p>The modulus operator turns out t be surprisingly useful. For example, you can check whether one number is divisible by another: if <code>x % y<\/code> is zero, then <code>x<\/code> is divisible by <code>y<\/code>.<\/p>\n<p>You can also extract the right-most digit or digits from a number.<br \/>\nFor example, <code>x % 10<\/code> yields the right-most digit of <code>x<\/code> (in base 10).<br \/>\nSimilarly, <code>x % 100<\/code> yields the last two digits.<\/p>\n<h3>String operations<\/h3>\n<p>The <code>+<\/code> operator works with strings, but it is not addition in the mathematical sense.<br \/>\nInstead it performs concatenation, which means joining the string by linking them end to end.<br \/>\nFor example:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; first = 10\n>&gt;&gt; second = 15\n>&gt;&gt; print(first+second)\n25\n>&gt;&gt; first = &#039;100&#039;\n>&gt;&gt; second = &#039;150&#039;\n>&gt;&gt; print(first + second)\n100150<\/code><\/pre>\n<p>The <code>*<\/code> operator also works with strings by multiplying the content of a string by an integer.<br \/>\nFor example:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; first = &#039;Test &#039;\n>&gt;&gt; second = 3\n>&gt;&gt; print(first * second)\nTest Test Test<\/code><\/pre>\n<h3>Asking the user for input<\/h3>\n<p>Sometimes we would like to take a value for a variable from the user via their keyboard.<br \/>\nPython provides a built-in function called <code>input<\/code> that gets input from the keyboard.<br \/>\nWhen this function is called, the program stops and waits for the user to type something.<br \/>\nWhen the user presses <code>Return<\/code> or <code>Enter<\/code>, the program resumes and <code>input<\/code> returns what user typed as a string.<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; inp = input()\nSome silly stuff\n>&gt;&gt; print(inp)\nSome silly stuff<\/code><\/pre>\n<p>Befor getting input from the user, it is a good idea to print a prompt telling the user what to input.<br \/>\nYou can pass a string to <code>input<\/code> to be displayed to the user before pausing for input:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; name = input(&#039;What is your name?\\n&#039;)\nWhat is your name?\nChuck\n>&gt;&gt; print(name)\nChuck<\/code><\/pre>\n<p>The senquence <code>\\n<\/code> at the end of the prompt represents a newline, which is a special charater that cause a line break.<br \/>\nThat's why the user's input appears below the prompt.<\/p>\n<p>If you expect the user to type an integer, you can try to convert the return value to <code>int<\/code> using the <code>int()<\/code> function:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; prompt = &#039;What...is the airspeed velocity of an unladen swallow?\\n&#039;\n>&gt;&gt; speed = input(prompt)\nWhat...is the airspeed velocity of an unladen swallow?\n17\n>&gt;&gt; int(speed)\n17\n>&gt;&gt; int(speed) + 5\n22<\/code><\/pre>\n<p>But if the user types something other than a string of digits, you get an error:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; speed = input(prompt)\nWhat...is the airspeed velocity of an unladen swallow?\nWhat do you mean, an African or a European swallow?\n>&gt;&gt; int(speed)\nValueError: invalid literal for int() with\nbase 10: &#039;What do you mean, an African or a European swallow?&#039;<\/code><\/pre>\n<p>We will see how to handle this kind of error later.<\/p>\n<h3>Comments<\/h3>\n<p>As programs get bigger and more complicated, they get more difficult to read.<br \/>\nFormal language are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why.<\/p>\n<p>For this reason, it is a good idea to add notes to your programs to explain in natrual language what the program is dong. Thest notes are called comments, and in python they start with <code>#<\/code> symbol:<\/p>\n<pre><code class=\"language-python\"># compute the percentage of the hour that has elapsed\npercentage = (minute * 100) \/ 60<\/code><\/pre>\n<p>In this case, the comment appears on a line by itself. You can also put commands at the end of a line:<\/p>\n<pre><code class=\"language-python\">percentage = (minute * 100) \/ 60     # percentage of an hour<\/code><\/pre>\n<p>Everything from the <code>#<\/code> to the end of the line is ignored; it has no effect on the program.<\/p>\n<p>Comments are most useful when they document non-abvious features of the code.<br \/>\nIt is reasonable to assume that the reader can figure out what the code does; it is much more useful to explain why.<\/p>\n<p>This comments is redundant with the code and useless:<\/p>\n<pre><code class=\"language-python\">v = 5   # assign 5 to v<\/code><\/pre>\n<p>This comment contains useful information that is not in the code:<\/p>\n<pre><code class=\"language-python\">v = 5   # velocity in meters\/second<\/code><\/pre>\n<p>Good variable names can reduce the need for comments, but long names can make complex expressions hart to read, so there is a trade-off.<\/p>\n<h3>Choosing mnemonic variable names<\/h3>\n<p>As long as you follow the simple rules of variable naming, and avoid reserved words, you have a lot of choice when you name your variables. In the beginning, this choice can confusing both when you read a program and when you write your own programs. the following three programs are identical in terms of what they accomplish, but they different when you read them and try to understand them.<\/p>\n<pre><code class=\"language-python\">a = 35.0\nb = 12.50\nc = a * b\nprint(c)<\/code><\/pre>\n<pre><code class=\"language-python\">hours = 35.0\nrate = 12.50\npay = hours * rate\nprint(pay)<\/code><\/pre>\n<pre><code class=\"language-python\">x1q3z9ahd = 35.0\nx1q3z9afd = 12.50\nx1q3p9afd = x1q3z9ahd * x1q3z9afd\nprint(x1q3p9afd)<\/code><\/pre>\n<p>The Python interpreter sees all three of these programs as exactly the same but humans see and understand these programs quite differently. Humans will most quickly understand the intent of the second program because the programer has chosen variable names that reflect their intent regarding what data will be stored in each variable.<\/p>\n<p>We call these wisely chosen variable names &quot;mnemonic variable names&quot;. The word mnemonic means &quot;memory aid&quot;.<br \/>\nWe choose mnemonic variable names to help us remember why we created the variable in the first place.<\/p>\n<p>While this all sounds great, and it is a very good idea to use mnemonic variable names, mnemonic variable names can get in the way of a beginning programmer's ability to parse and understand code. This is because beginning programmers have not yet memorized the reserved words (there are only 35 of them) and sometimes variable with names that are too descriptive start to look like part of the language and not just well-chosen variable names.<\/p>\n<p>Take a quick look at the following Python sample code which loops through some data. We will cover loop soon, but for now try to puzzle through what this means:<\/p>\n<pre><code class=\"language-python\">for word in words:\n    print(word)<\/code><\/pre>\n<p>What is happening here? Which of the tokens (for,word,in,etc.) are reserved words and wich are just variable name? Does Python understand at a fundamental level the notion of words? Beginning programmers have trouble separating what parts of the code must be the same as this example and what parts of the code are simply choices made by programmer.<\/p>\n<p>The following code is equivalent to the above code:<\/p>\n<pre><code class=\"language-python\">for slice in pizza:\n    print(slice)<\/code><\/pre>\n<p>It is easier for the beginner programmer to look at this code and know which parts are reserved words defined by Python and which parts are simply variable names chosen by the programmer. It is pretty clear that Python has no fundamental understanding of pizza and slices and the fact that a pizza consists of a set of one or more slices.<\/p>\n<p>But if our program is truly about reading data and looking for words in the data, <code>pizza<\/code> and <code>slice<\/code> are very unmnemonic variable names. Choosing them as variable names distracts from the meaning of the program.<\/p>\n<p>After a pretty short period of time, you will know the most common reserved words and you will start to see the reserved words jumping out at you:<\/p>\n<pre><code class=\"language-python\">for word in words:\n    print(word)<\/code><\/pre>\n<p>The parts of the code that are defined by Python (<code>for<\/code>,<code>in<\/code>,<code>print<\/code>,and <code>:<\/code>) are in bold and the programmer-chosen variable(<code>word<\/code> and <code>words<\/code>) are not in bold. Many text editors are aware of Python syntax and will color reserved words differently to give you clues to keep your variables and reversed words separate. After a while you will begin to read Python and quickly determine what is a variable and what is a reserved word.<\/p>\n<h3>Debugging<\/h3>\n<p>At this point, the syntax error you are most likely to make is an illegal variable name, like <code>class<\/code> and <code>yield<\/code>, which are keywords, or <code>odd~job<\/code> and <code>US$<\/code>, wich contain illegal charaters.<\/p>\n<p>If you put a space in a variable name, Python thinks it is two operands without an operator:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; bad name = 5\nSyntaxError: invalid syntax<\/code><\/pre>\n<p>For syntax error, the error messages don't help much. The most common message are <code>SyntaxError: invalid syntax<\/code> which is not very informative.<\/p>\n<p>The runtime error you are most likely to make is a &quot;use befor def;&quot; that is, trying to use a variable before you have assigned a value. This can happen if you spell a variable name wrong:<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; principal = 327.68\n>&gt;&gt; interest = principle * rate\nNameError: name &#039;principle&#039; is not defined<\/code><\/pre>\n<p>Variable names are case sensitive, so <code>LaTex<\/code> is not the name as <code>latex<\/code>.<\/p>\n<p>At this point, the most likely cause of a semantic error is the order of operations.<br \/>\nFor example, to evaluate 1\/2\u03c0, you might be tempted to write<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; 1.0 \/ 2.0 * pi<\/code><\/pre>\n<p>But the division happens first, so you would get \u03c0\/2, which is not the same thing!<br \/>\nThere is no way for Python to know what you meant to write, so in this case you don't get an error message; you just get the wrong answer.<\/p>\n<h3>Glossary<\/h3>\n<p><strong>assignment<\/strong><br \/>\nA statement that assign a value to a variable.<\/p>\n<p><strong>concatenate<\/strong><br \/>\nTo join two operands end to end.<\/p>\n<p><strong>comment<\/strong><br \/>\nInformation in a program that is for other programmer (or anyone reading the souce code) and has no effect on the execution of the program.<\/p>\n<p><strong>evaluate<\/strong><br \/>\nTo simplify an expression by performing the operations in order to yield a single value.<\/p>\n<p><strong>expression<\/strong><br \/>\na combination of variables, operators, and values that represents a single result value.<\/p>\n<p><strong>floating point<\/strong><br \/>\nA type that represents numbers with fractional parts.<\/p>\n<p><strong>integer<\/strong><br \/>\nA type that represents whole numbers.<\/p>\n<p><strong>keyword<\/strong><br \/>\nA reserved word that is used by the compiler to parse a program; you cannot use keyword like <code>if<\/code>, <code>def<\/code>, and <code>while<\/code> as variable names.<\/p>\n<p><strong>mnemonic<\/strong><br \/>\nA memory aid. We often give variables mnemonic names to help us remember what is stored in the variable.<\/p>\n<p><strong>modulus operator<\/strong><br \/>\nAn operator, denoted with a percent sign (%), that works integers and yields the remainder when one number is divided by another.<\/p>\n<p><strong>operands<\/strong><br \/>\nOne of the values on which an operator operates.<\/p>\n<p><strong>operator<\/strong><br \/>\nA special symbol that represents a simple comutation like addition, multiplication, or string concatenation.<\/p>\n<p><strong>rules of precedence<\/strong><br \/>\nThe set of rules governing the order in which expression involving multiple operators and operands are evaluated.<\/p>\n<p><strong>statement<\/strong><br \/>\nA section of code that represents a command or action. So far,the statements we have seen are assignments and print expression statement.<\/p>\n<p><strong>string<\/strong><br \/>\nA type that represents sequences of charaters.<\/p>\n<p><strong>type<\/strong><br \/>\nA category of values. The types we have seen so far are integer(type <code>int<\/code>), floating-point numbers(type <code>float<\/code>), and string(type <code>str<\/code>)<\/p>\n<p><strong>value<\/strong><br \/>\nOne of the basic units of data, like a number or string, that a program manipulates.<\/p>\n<p><strong>variable<\/strong><br \/>\nA name that refers to a value.<\/p>\n<h3>Exercises<\/h3>\n<p><strong>Exercise 2:<\/strong> Write a program that uses <code>input<\/code> to prompt a user for their name and then welcomes them.<\/p>\n<pre><code class=\"language-python\">Enter your name: Chuck\nHello Chuck<\/code><\/pre>\n<pre><code class=\"language-python\">inp = input(&#039;Enter your name: &#039;)\nprint(&#039;Hello&#039;, inp)<\/code><\/pre>\n<p><strong>Exercise 3:<\/strong> Write a program to prompt the user for hours and rate per hour to compute gross pay.<\/p>\n<pre><code class=\"language-python\">Enter Hours: 35\nEnter Rate: 2.75\nPay: 96.25<\/code><\/pre>\n<pre><code class=\"language-python\">hours = float(input(&#039;Enter Hours: &#039;))\nrate = float(input(&#039;Enter Rate: &#039;))\nprint(&#039;Pay: &#039;, hours * rate)<\/code><\/pre>\n<p>We won't worry about making sure our pay has exactly two digits after the decimal place for now. If you want, you can play with the built-in Python <code>round<\/code> function to properly round the resulting pay to two decimal places.<\/p>\n<pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165\u5e76\u8f6c\u6362\u4e3a\u6570\u503c\nhours = float(input(&quot;\u8bf7\u8f93\u5165\u5de5\u4f5c\u65f6\u957f\uff1a&quot;))\nrate = float(input(&quot;\u8bf7\u8f93\u5165\u65f6\u85aa\uff1a&quot;))\n\n# \u8ba1\u7b97\u603b\u5de5\u8d44\u5e76\u56db\u820d\u4e94\u5165\u5230\u4e24\u4f4d\u5c0f\u6570\ngross_pay = round(hours * rate, 2)\n\n# \u8f93\u51fa\u7ed3\u679c\nprint(&quot;\u603b\u5de5\u8d44\u4e3a\uff1a&quot;, gross_pay)<\/code><\/pre>\n<p><strong>Exercise 4:<\/strong> Assume that we execute the following assignment statements:<\/p>\n<pre><code class=\"language-python\">width = 17\nheight = 12.0<\/code><\/pre>\n<p>For each of the following expressions, write the value of the expression and the type (of the value of the expression).<\/p>\n<ol>\n<li><code>width\/\/2<\/code>  8  int<\/li>\n<li><code>width\/2.0<\/code>  8.5  float<\/li>\n<li><code>height\/3<\/code>  4.0  float<\/li>\n<li><code>1 + 2 * 5<\/code>  11  int<\/li>\n<\/ol>\n<p><strong>Exercise 5:<\/strong> Write a program which prompts the user for a Celsius temperature, convert the temperature to Fahrenheit, and print out the converted temperature.<\/p>\n<pre><code class=\"language-python\">inp = input(&#039;Enter Celsius Temperature: &#039;)\ncel = float(inp)\nfahr = (cel * 9.0) \/ 5.0 + 32.0\nprint(fahr)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Variables, expressions, and statements values and types&#8230; &raquo; <a class=\"read-more-link\" href=\"http:\/\/blue.yn.cn\/?p=2419\">\u9605\u8bfb\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-2419","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2419"}],"version-history":[{"count":15,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2419\/revisions"}],"predecessor-version":[{"id":2608,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/2419\/revisions\/2608"}],"wp:attachment":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2419"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}