read.cash Log in
S@Scripts more from that month

Introduction to Python

Introduction Python is a high-level programming language for general-purpose programming. It is open source. It is widely use on a machine learning, data science and scripting. Why Python? Python was my very first language, and it is easy to learn and easy to use. It is used by various industries and companies (Google, Facebook, Instagram, Etc.). It has been used to develop web applications, system administration, desktop applications, and specially machine learning and data science. It is embraced in Data science and Machine learning community. Python Shell Python is an interpreted scripting language, so it doesn't need to be compiled. It executes the code line by line. When you enter the code, it interprets the code and shows the result in the next line. Open your terminal or command prompt(cmd) and write: python The python interactive shell is opened and it is waiting for you to write some codes. Lets write our very first code on the python shell. >>> 2 + 3 5 In order for you to exit the interactive shell write **exit()** command and press Enter. exit() Now, we know how to open and exit the interactive shell. Python will give you results if you write scripts that python understands, if not it returns errors. Let's practice more how to use python interactive shell. Go to your terminal or command prompt(cmd) and write the word **python**. The python interactive shell is opened. Let's do some basic mathematic operations (addition, subtraction, multiplication, division, modulus, exponential). Lets do some maths first before we write any python code: 1 + 1 = 2 5 - 2 = 3 2 * 4 = 8 5 / 2 = 2.5 3 ** 2 = 9 In python we have the following additional operations: 5 % 3 = 2-> means finding the remainder 3 // 2 = 1 -> means removing the remainder Open the python interactive shell and lets write a comment at the very beginning of the shell. A *comment* is a part of the code that the python won't execute. We can leave a reminder or text in our code to make our code more readable. Python won't run the comment part. In order for us to comment in python it should start with a hash (#) symbol. Example of a comment: # This is a comment # Be sure to start with a hash Sample code: >>> 1 + 1 # addition(+) 2 >>> 5 - 2 # subtractio(-) 3 >>> 2 * 4 # multiplication(*) 8 >>> 5 / 2 # division(/) 2.5 >>> 5 % 3 # Modulus(%) - finding the remainder 2 >>> 3 % 5 # Modulus(%) - finding the remainder 3 >>> 3 // 2 # Floor division operator(//) - it removes the remainder 1 >>> 3 ** 2 # Exponential Operator(**) 0 equivalent to 3^2 or 3 * 3 Now we learned how the python interactive shell works. Lets close the opened shell by writing *exit()* on the shell and open it again, lets practice how to write text on python shell. >>> 'Hello World' # write hello world string 'Hello World' >>> "Hello World" # hello world with double quote 'Hello World' >>> 'This is a text that we wrote' 'This is a text that we wrote' >>> # We can also use a triple quotes if its more than one line('''''') >>> '''This is the use of triple quote if the text is more than one line''' 'This is the use of triple quote if the text is more than one line' >>> # Lets continue using a text editor / code editor >>> exit() Now lets proceed to the development environment. Download the VScode or your ideal code editor it can be IDE or just a normal text editor you can even code on a notepad if you want. Anyways lets start coding. Basic Python Python Syntax A Python script can be written in python interactive shell or in the code editor. A python file has an extension of .py. Python Indentation Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. Comments Comments can be used as a reminder, and a notes for you to easily understand your code. Python won't be running a comment, thus making your code more neat and more readable. **Example of a comments:** # First comment # Second comment # Third comment **Multiline Comment** Multiline comment uses a triple quotes: """This is a multiline comment it does take a lot of line hello world """ **Data types** Of course, python also have several types of data types. **Number** Integer: Integer(negative, zero and positive) numbers Example: ... -3, -2, -1, 0, 1, 2, 3 ... Float: Decimal number Example ... -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5 ... Complex Example 1 + j, 2 + 4j **String** A collection of one or more characters under a single or double quote. If a string is more than one sentence then we use a triple quote. **Example:** 'Carl' "Justin" """Carl Justin Gamos""" **Booleans** Data type that uses True or False(Always Uppercase). **Examples:** True # when the value is true False # when the value is false **List** It is a collection of an array similar to JavaScript, C++, and Java. **Examples:** [0, 1, 2, 3, 4, 5] # a list of numbers ['Sta Ana', 'Porac', 'Bacolor', 'Floridablanca'] # a list of strings ['Carl', 30, True, 9.81] # a list of different data types **Dictionary** A python dictionary object is an unordered collection of data. **Examples:** {'name': 'Kepler', 'country':'Philippines', age:20, 'is_married':False} **Tuple** Is an ordered collection of data types like list but cannot be modified. They are immutable. **Examples:** ('Carl', 'Justin', 'Cinco', 'Gamos') **Set** Is a collection of data types similar to list and tuple. set is not an ordered collection of items, it stores only unique items. We will go in detail about each and every python data type. **Examples:** {3.14, 9.81, 2.7} # order is not important in set Checking Data types >>> type(10) <class 'int'> >>> type(3.14) <class 'float'> >>> type(1 +3j) <class 'complex'> >>> type('Carl') <class 'str'> >>> type([1,2,3]) <class 'list'> >>> type({'name':'carl'}) <class 'dict'> >>> type((1,2,3,4,5,6)) <class 'tuple'> >>> type({0.1,3.1,4.15}) <class 'set'> Python File Create a folder. Inside the folder create a file called helloworld.py and do all the things we did in the python interactive shell using the visual studio code or your text editor or IDE of your choice. Run the following code: print(2 + 3) # addition(+) print(3 - 1) # subtraction(-) print(2 * 3) # multiplication(*) print(3 / 2) # division(/) print(3 ** 2) # exponential(**) print(3 % 2) # modulus(%) print(3 // 2) # Floor division operator(//) # Checking data types print(type(10)) # Int print(type(3.14)) # Float print(type(1 + 3j)) # Complex number print(type('Carl')) # String print(type([1, 2, 3])) # List print(type({'name':'Carl'})) # Dictionary print(type({9.8, 3.14, 2.7})) # Set print(type((9.8, 3.14, 2.7))) # Tuple Open your terminal, then locate the file. In the terminal type *python helloworld.py* then press **Enter**. Congrats! 🎉More to come more to code and hopefully this keeps coming. I will try to motivate my self more to code more and not to slack off HAHAHA! Anyways always remember that what *Martin Fowler* said: “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler 💻 Exercises Check the python version you are using be sure its python 3 and above. Open the python interactive shell and do the following operations. The operands are 3 and 4. Check the example above Addition(+) Subtractions(-) Multiplication(*) Modulus(%) Division(/) Exponential(**) Floor Division Operator(//) Write strings on the python interactive shell. The strings are the following: Your name Your family name Your country Check the data types of the following data: 10 9.8 3.14 5 - 5k ['Hello', 'World', 'Python'] Your name Your Family Name Your country

No comments yet

Log in to join in Reading is open to everyone. Replying needs an account.