Python Programming : Chapter 1


First of all, what is Python? According to its creator, Guido van Rossum, Python is a:
“high-level programming language, and its core design philosophy is all about code readability and a syntax which allows programmers to express concepts in a few lines of code.”
For me, the first reason to learn Python was that it is, in fact, a beautiful programming language. It was really natural to code in it and express my thoughts.

Another reason was that we can use coding in Python in multiple ways: data science, web development, and machine learning all shine here. Quora, Pinterest and Spotify all use Python for their backend web development. So let’s learn a bit about it.

Hello World!

If programming is the act of teaching a computer to have a conversation with a user, it would be most useful to first teach the computer how to speak. In Python, this is accomplished with the printstatement.
print "Hello, world!" print "Water—there is not a drop of water there! Were Niagara but a cataract of sand, would you travel your thousand miles to see it?"
print statement is the easiest way to get your Python program to communicate with you. Being able to command this communication will be one of the most valuable tools in your programming toolbox.

Print Statements

There are two different Python versions. Both Python 2 and Python 3 are used throughout the globe. The most significant difference between the two is how you write a print statement. In Python 3, print has parentheses.
print("Hello World!") print("Deep into distant woodlands winds a mazy way, reaching to overlapping spurs of mountains bathed in their hill-side blue.")
In this course we will be using Python 2. If you go on to write Python 3 it will be useful to note this key difference.


When printing things in Python, we are supplying a text block that we want to be printed. Text in Python is considered a specific type of data called a string. A string, so named because they're a series of letters, numbers, or symbols connected in order — as if threaded together by string. Strings can be defined in different ways:
print "This is a good string" print 'You can use single quotes or double quotes for a string'
Above we printed two things that are strings and then attempted to print two things that are not strings. While double-quotes (") and single-quotes (') 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 combine multiple strings using +, like so:
print "This is " + "a good string"
This code will print out "This is a good string".

Handling Errors

As we get more familiar with the Python programming language, we run into errors and exceptions. These are complaints that Python makes when it doesn't understand what you want it to do. Everyone runs into these issues, so it is a good habit to read and understand them. Here are some common errors that we might run into when printing strings:
print "Mismatched quotes will cause a SyntaxError' print Without quotes will cause a NameError
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 (called an EOL) before the double-quote that was supposed to close the string appeared. The program will abruptly stop running with the following message:
SyntaxError: EOL while scanning a string literal
This means that a string wasn't closed, or wasn't closed with the same quote-character that started it.
Another issue you might run into is attempting to create a string without quotes at all. Python treats words not in quotes as commands, like the printstatement. If it fails to recognize these words as defined (in Python or by your program elsewhere) Python will complain the code has a NameError. This means that Python found what it thinks is a command, but doesn't know what it means because it's not defined anywhere.


In Python, and when programming in general, we need to build systems for dealing with data that changes over time. That data could be the location of a plane, or the time of day, or the television show you're currently watching. The only important thing is that it may be different at different times. Python uses variables to define things that are subject to change.
greeting_message = "Welcome to Codecademy!" current_excercise = 5
In the above example, we defined 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.


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, and Python is no exception. Some examples:
mirthful_addition = 12381 + 91817 amazing_subtraction = 981 - 312 trippy_multiplication = 38 * 902 happy_division = 540 / 45 sassy_combinations = 129 * 1345 + 120 / 6 - 12
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 order 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.
is_this_number_odd = 15 % 2 is_this_number_divisible_by_seven = 133 % 7
In the above code block, we use the modulo operator to find the remainder of 15 divided by 2. Since 15 is an odd number the remainder is 1.
We also check the remainder of 133 / 7. Since 133 divided by 7 has no remainder, 133 % 7 evaluates to 0.

Updating Variables

Changing the contents of a variable is one of the essential operations. As the flow of a program progresses, data should be updated to reflect changes that have happened.
fish_in_clarks_pond = 50 print "Catching fish" number_of_fish_caught = 10 fish_in_clarks_pond = fish_in_clarks_pond - number_of_fish_caught
In the above example, we start with 50 fish in a local pond. After catching 10 fish, we update the number of fish in the pond to be the original number of fish in the pond minus the number of fish caught. At the end of this code block, the variable fish_in_clarks_pond is equal to 40.
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.
money_in_wallet = 40 sandwich_price = 7.50 sales_tax = .08 * sandwich_price sandwich_price += sales_tax money_in_wallet -= sandwich_price
In the above example, we use the price of a sandwich to calculate sales tax. After calculating the tax we add it to the total price of the sandwich. Finally, we complete the transaction by reducing our money_in_wallet by the cost of the sandwich (with tax).

We're trying to figure out how much it rained in the past year! Update the annual_rainfallvariable to include the values from September to December.

january_to_june_rainfall = 1.93 + 0.71 + 3.53 + 3.41 + 3.69 + 4.50 annual_rainfall = january_to_june_rainfall july_rainfall = 1.05 annual_rainfall += july_rainfall august_rainfall = 4.91 annual_rainfall += august_rainfall september_rainfall = 5.16 annual_rainfall += september_rainfall october_rainfall = 7.20 annual_rainfall += october_rainfall november_rainfall = 5.06 annual_rainfall += november_rainfall december_rainfall = 4.06 annual_rainfall += december_rainfall


Most of the time, code should be written in such a way that it is 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. When you look back at your code later, comments may help you figure out what it was intended to do.
# this variable counts how many rows of the spreadsheet we have: row_count = 13


Post a Comment