Code Academy Part 1 First section on Coding From Code Academy Python Code Academy (Self Teaching)
S.1: Programming is teaching the computer to have a conversation with a user. First exercise is how to make the computer to speak and the way we do this is through the statement PRINT.
Example: Print “Hello world”
A Print statement is the easiest way to get your computer to communicate with you. Therefore, being able to command this will be valuable with tools in your programming toolbox.
S.1.1 Print Statements
Two different types of Python. Version 2 and Version 3. The difference is how you write the Print Statement. In Python 3, PRINT has parentheses. Parentheses are a pari of signs that are used in writing to mark off an interjected explanatory or qualifying remark.
S.1.2 Strings When making things print in Python, we make a text block that we want to be printed. Text in Python are called a String. A String are a series of letters, numbers or symbols connected in a order. You can use both double quotes and single quotes and they are both acceptable ways to define a String, a String needs to be opened and closed by the same type of quote mark. We can also combine strings with the plus sign:
print “This is ” + “ a good string”
S.1.3 Handling Errors
EOL: If the quotes are mismatched Python will notice this and inform you that your code has an error in its Syntax because the line ended. Before the double- quote that was supposed to close the string appeared. The program will abruptly stop running and give you and error message.
S.1.4 Variables Within Python and when programming in general, we need to build systems that deal with data changes over time. This data could be the location of a plane, or the time of day, or the TV show your currently watching. Python uses variables to define things that are subject to change. Example: Define a variable called greeting_message and set it equal to the String “Welcome to codecademy. It also defined a variable called current_exercise and set it equal to the number 5. Arithmetic One thing computers are capable of doing exceptionally well is performing arithmetic. Addition, subtraction, multiplication, division, and other numeric calculations are easy to do in most programming languages. Above are a number of arithmetic operations, each assigned to a variable. The variable will hold the final result of each operation. Combinations of arithmetical operators follow the usual or der of operations. Python also offers a companion to division called the modulo operator. The modulo operator is indicated by % and returns the remainder after division is performed. S.1.5 Updating Variables Changing the contents of a variable is one of the essential operations. When the flow of the data flows through the program progresses, data should be updated to reflect changes that have happened. Updating a variable by adding or subtracting a number to the original contents of the variable has its own shorthand to make it faster and easier to read. S.1.6 Comments Code should be written in such a way so that it’s easy to understand on its own. However, if you want to include a piece of information to explain a part of your code, you can use the # sign. A line of text preceded by a # is called a comment. The machine does not run this code - it is only for humans to read. Therefore, you can go back to the code later, comments may help you figure out what it was intended to do.
S.1.7 Numbers Variables can also hold values. The simplest kind of number in Python is the integer, which is a whole number with no decimal point. Int1 = 1 int2 = 10 int3 = 5 A number that has a decimal point is called a float. You can define floats with numbers after the decimal point or by just including a decimal point at the end. Float1 = 1.0 float2 = 10. float3 = 5.5
Furthermore, you can also define floats using scientific notation, with indication the power of 10. #this evaluates to 150: float4 = 1.5e2 Example: cumbers = 3 price_per_cucumber = 3.25 total_cost = cucumbers * price_per_cucumber print total cost
Two Types of Division When we divide two integers, we get an integer as a result. When the quotient is a whole number, this works fine. But if the numbers do not divide evenly the result of the division is truncated into an integer. In other words, the quotient is rounded down to a whole number. This can be surprising when you expect to receive a decimal and you receive a rounded-down integer. To yield a float as the result instead, programmers often change either the numerator or the denominator to be a float.
Multi-Line Strings
Define a string with single quotes and with double quotes. If we want a string to span multiple line, we can also use triple quotes. This address spans multiple lines and still contained in one variable, address_string. When a string like this is not assigned to a variable, it works as a multi-line comment. This can be helpful as your code gets more complex.
Booleans Variables that are either true or false. This datatype, which can only ever take one of two values, is called Boolean. In Python, we define Booleans using the keywords True and False.
A Boolean is actually a special case of an integer. A value of True corresponds to an integer value of 1 and will behave the same. A value of False corresponds to an integer of 0.
Value Error A variable with the value 7 is an integer, 7. is a float, "7” is a string. Sometimes we will want to convert variables to different datatypes. For example, if we wanted to print out an integer as part of a string. For example, we wanted to print out an integer as part of a string, we would want to convert that integer to a string first. We can do that using str()
Similarly, if we have a string like “7” and we want to perform arithmetic operations on it, we must convert it to a numeric datatype. We can do this using int()
If you use int() on a floating point number, it will round the number down.