<jupyter_start><jupyter_text>CHAPTER-2 : Variables, Expressions and Statements  **Variables**One of the most powerful features of a programming language the ability to manipulate **variables**. A variable is a name that refers to a **value**.  **Assignment Statements**An assignment statement creates a new variable and give it a value:<jupyter_code>message = 'And now for something completely different'                  #string to a new variable named message
n = 17                                                                  #integer 17 to n
pi = 3.141592653589793                                                  #assigns the appproximate value of pi
# with arrow marking in paper is called state diagram<jupyter_output><empty_output><jupyter_text>**Variable Names**1. Programmers generally choose names for their variables that are meaningful 2. Variable names can be as long as you like. They can contain both letters and numbers, but they can't begin with a number.3. It is legal to use uppercase letters, but it is conventional to use only lowercase for variables names.4. Underscore character _, can appear in a name it is legal.<jupyter_code>#If you give a variable an illegal name, you get a syntax error:

76trombones = 'big parade'              # illegal because it begins with a number.
more@ = 1000000                          # illegal because it contains illegal character, @.
class = 'Advanced Theoretical Zymurgy'  # illegal because It turns out that class is one of python's keywords<jupyter_output><empty_output><jupyter_text>**Python 3 Keywords**(False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yeild )  **Expressions and Statements**An expressions is a combination of vakues, variables, and operators.<jupyter_code>42
n
n + 25
# When you type an expression at the prompt, the interpreter evaluates it. which means that it finds the value  of the expression. 
# In this example, n has the value 17 and n + 25 has the value 42

# A Statement is a unit of code that has an effect, like creating a variable or displaying a value.
n = 17      # assignment statement that give a values to n.
print(n)    # Print statement that displays the value of n.
# When yo type a statement, the interpreter executes it, which means that it does whatever the statement says. 
# In general, statements don't have values.<jupyter_output>17<jupyter_text>**Script Mode**1. So far we have run python in **interactive mode**, which means the you interact directly with the interpreter.2. Interactive mode is good way to get started, but if you are working with more than a few lines of code, it can be clumsy.3. The alternative is to save code in  a file called a script and then run the interpreter in script mode to execute the script. By Convention.4. Python scripts have names that end with **.py**.<jupyter_code>miles = 26.2
miles * 1.61
# The first line assigns  a value to miles,but it has no visible effect.
# The second line  is an expression, so the interpreter evaluates it and displays the result.
# It turns out that a marathon is about 42 kilometers.


# But if you type the same code into  a script and run it ,you get no output at all.
# In script mode an expression, all by itself, has no visible effect
# Python actually evaluates the expression, but it doesnt display the value unless you tell it to:

miles = 26.2
print(miles * 1.61)

# This behaviour can be confusing at first.
# A script usually contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.

# for example the script
print(1)
x = 2
print(x)
# The assignment statment produces no output.
5
x=5
x+1<jupyter_output><empty_output><jupyter_text>**Order of Operations** 1. When an expression contains more than one operator, the order of evaluation depends on the **order of operations**.2. For mathematical operators, python follows mathematical convention.3. The acronym **PEMDAS** is a useful way to remember the rules:**PEMDAS**-  **Parentheses** have the highest precedence and can be used to force an  expression to evaluate in the order you want. Since expression in parentheses are evaluated.  - first, 2 * (3-1) is 4 and (1+1) ** (5-2) is 8 .  - You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60. even if it doesnt change the result.- **Exponentiation** has the next highest precedence, so 1 + 2**3 is 9, not 27, and 2* 3**2 is 18, not 36.- **Multiplication** and **Division** have higher  precedence than **Addition** and **Substraction**   - so 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.- operators eith the same precedence are evaluated from left to right(except exponentiation).   - so in the expression degrees / 2 * pt, the division happens first and the result is multiplied by pi.   - To divide by 2pie, you can use parentheses or write degrees / 2 / pi.    **String Operations** - In general, you cant perform mathematical operatiions on strings, even if the strings look like numbers so the following are illegal:    * '2'-'1'   'eggs'/'easy'   'third'*'a charm'- But there are two exceptions, + and *.- The + operator performs  string concatenation, which means it joins the strings by linking them end to end.- The * operator also works on strings it perform repetition.    * If one othe values is a string, the other has to be an integer.<jupyter_code># For example + operator
first = 'throat'
second = 'warbler'
first + second

# For example * operator
'Spam' * 3<jupyter_output><empty_output><jupyter_text>- This use of + and * makes sense by analogy with addition and multiplication.- Just as 4*3 is equivalent to 4+4+4, we expect 'Spam'*3 to be the same as 'Spam'+'Spam'+'Spam', and it is.- On the other hand, there is a significant way in whichj string concatenation and repetitiion are different from integer addition and multiplication.- Can you think of a property that addition has that string concatenation des not?  **Comments** Good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called **comments**, and they start with the **** symbol:<jupyter_code># computr the percentage of the hour that elapsed
percentage = (minute * 100) / 60
percentage = (minute * 100) / 60 # percentage of an hour<jupyter_output><empty_output><jupyter_text>**Debugging** Three kinds of errors can occur in a program1. Syntax error:   - Syntax refers to the structure of a program and the rules about that structure.   - For example parentheses have to come in matching pairs, so(1 + 2) is  legal, but 8) is a syntax error.   - If thre is a syntax error anywhere in your program, Python displays an error message and quits,  and you will not be able to run the program.2. Runtime error:   - Runtime error refers to the error does not appear until after the program has started running.   - These errors are also called **exceptions** because they usually indicate that something exceptional (and bad) has happened.3. Semantic erroe:   - Semantiic which means related to meaning.   - If there is a semantic error in your program, it will do something else. Specifically, it will do what you told it to do.   - Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing.  question 11. **The volume of a sphere with radius r is 4/3 pie r power of 3. what is the volume of a sphere with radius 5**<jupyter_code>r = 5
v = 4/3 * 3.14159 * 5**3
v<jupyter_output><empty_output><jupyter_text>question 22. **Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping cost $3 for the first copy amd 75 cents for each additional copy.What is the total wholesale cost for 60 copies?**<jupyter_code>Book_price = 24.95
discount = Book_price * (40/100)
d_price = Book_price - discount
shipping = 3 + (0.75 * (60 - 1))
wholesale = d_price * 60 + shipping
wholesale<jupyter_output><empty_output><jupyter_text>questio 33. **if i leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then  3 miles at tempo (7;12 per mile) and 1 mile at an easy pace again, what time do i  get home for breakfast?**<jupyter_code>SECONDS = 1
MINUTES = 60 * SECONDS
HOURS = 60 * MINUTES

# All these results are in seconds

time_left_house = 6 * HOURS + 52 * MINUTES

miles_run_easy_pace = 2 * (8 * MINUTES + 15 * SECONDS)

miles_run_fast_pace = 3 * (7 * MINUTES + 12 * SECONDS)

total_time_run = miles_run_easy_pace + miles_run_fast_pace + time_left_house

# So we now have a big number of seconds to split into hours/minutes/seconds

hours = total_time_run // HOURS

# the left over part is minutes and seconds (still in seconds)

part_hour = total_time_run % HOURS
minutes = part_hour // MINUTES
seconds = part_hour % MINUTES

print ("Total time run: {}, Hours: {}, Minutes: {}, Seconds: {}".format(
    total_time_run, hours, minutes, seconds))<jupyter_output>Total time run: 27006, Hours: 7, Minutes: 30, Seconds: 6
{'fraction_non_alphanumeric': 0.07364975450081833, 'fraction_numerical': 0.024768139661756682, 'mean_word_length': 3.904226859283039, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '195904', 'license': 'MIT', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2478, 'n_words': 1335, 'path': 'Chapter_2/Variables,Expressions And Statements.ipynb', 'repo_name': 'Shrihith/Think-python', 'size': '9165'}