Learn to code - Super Simple way to learn the basics of Python

in #learn6 years ago (edited)

In this post I have covered everything that we need to know to get started coding in Python. It is broken down to super simple chunks of programming concepts. I hope this might help to refresh the basics or help you get started in writing python. So, lets dig into it starting with this meme:

What is Python and why learn it?

Python is a free general purpose programming language, which means that it can be used to build or program anything and everything with the help of correct tools or libraries. Python is easy to understand, fun to use with tons of great resources to get help from, and is a great backed tool for web development, AI, scientific computing, data analysis and much more. Not to mention, it has great career opportunities.

Operators

Airthmetic Operators

OperatorDescription
+adds
-subtracts
*multiplies
/divides left hand number by right hand
%modulus - divides left hand number by right hand and returns the remainder

Note: % is also used as placeholders. For example: %s

Relational/Comparison operators

Relational operator help us to compare values to get Boolean value(true or false). Every time we use relational operator we get a Boolean value back. Relational operators are 6 altogether >, <, >=,<=, ==, !=

Logical Operators

  • and operator
  • or operator

Dictionaries

Dictionaries are another data type which stores values except it has a key - we use {} brackets:

Tuples

Tuples are another data type where data cannot be changed.

If/Else statement and Nested If/Else statement

The concept of nested if/else is spacing or indexing. If one if/else statement is under another it's called nested.

Elif Statements

It comes in between IF and ELSE statement like - if day is Monday, print something, Elif day is Tuesday, print something, else print this.

Loops

Loops are code that are executed again and again until the condition becomes false. Loops makes your code run faster by executing group of statements multiple times rather one after another.

  • For loops: It uses RANGE function and can also iterate through a set of data like lists and Tuples. e.g. For something IN in RANGE (2,5,2): print (something)
  • Range function: It prints in range. It has 2 types. (1,5) or (1,7,1).
    **Tip - In order to print range in just one line instead line by line use - list function.
  • While loops: It repeatedly checks for one single condition, if that condition is true the code runs and if that condition is false the code or the while loop ends.

Pass, Break and Continue (BCP)

These are loop control statements. They deviate the loop from its normal sequence.

  • Break: All it does is terminates the execution and transfers the execution to the next. It will break out of a for or while loop.
  • Pass: It is a filler code. If you have if/statement and if part done and for else u use the pass code to fill it out .
  • Continue: It will bypass any code within a code written underneath it and will make sure the loop iterates over once again.

Try and Except

First we TRY to access the data and if any errors occur or something goes wrong then we can directly go to EXCEPT and tell the user that something is wrong.

Comments and escape character :

#single line comment
"""for multiple line comments also end with """
/before a ' and will make it as a comment

Functions:

Python functions are divided into 3 parts i.e. name, parameter and body.

  • name - In order to write a python function u have to start the function with the keyword called def followed by the name of the function fahr_to_celsius
  • parameter - now add the parameter that you want to pass through it later. Here we are just giving it a random name called (temp), you can name whatever you want. Make sure to put it in a parenthesis.
  • body - Now comes the body of the function. It starts with a keyword called return because it has to return or send the value going through whatever logic we created.

In the above function(image above) you can pass any number as the parameter value. The functions are executed like below:

sh fahr_to_celsius(152)

sh fahr_to_celsius(10)

sh fahr_to_celsius(32)

For above executed function the result will be 66.66, -12.221, 0 respectively.

We can either create our own functions or use the built-in functions. It works the same. Most of the functions that we will be using mostly are already built-in python libraries. If we couldn't find any, we can always create one. There are tons of free python libraries for specific projects. We can always use those libraries. Below are some of the important built-in functions that might be useful for our day-to-day programming tasks:

Built in functions

  • del - is used to delete
  • len - returns length of array
  • max - returns maximum value
  • min - returns minimum value
  • append - adds objects to list
  • count - counts, how many times the object is repeated
  • abs - absolute value makes negative number positive and positive to positive.
  • bool- it returns true if it's integer and if it's zero or null it returns false.
  • dir - it's the directory, it will give the list of functions which we can use.
  • help- it helps us to find out what each function does.
  • float() - it converts values including decimal values entered in positive number
  • Int()- it rounds up the decimal numbers in whole numbers.
  • len()- in strings it returns the how many items are in array and length of characters in string.
  • max()- it returns the largest number in the list or largest letter going through ascending order.
  • min()- it returns the opposite of max.
  • sum()- it adds the numbers

Note: For some functions dealing with arrays or lists - we use [ ] brackets: Arrays and lists can be used in Python to store any data type (real numbers, strings, etc)

Opening and reading txt files in IDLE

  • open() - open function will acknowledge the file but doesn't open it. Make sure u use the file path with / in between rather than \ . For e.g: C:/Users/ComputerName ...
  • read() - after open() function it opens and reads the file.
  • tell() - after we open the same file again it won't open it's because the pointer is at the last. We use tell function to know where the pointer is and it will return the line number.
  • seek (0,0) - it moves the pointer to the beginning and now we can read.
  • close() - it closes the txt file.

Copying text files

  • write() - this lets you write in the file but replaces the previous written words.
  • Import os - this is an operating system module. We can use it to rename text files.
  • Input - it let's input the file name. Note: have an extra space to not get error.

To be cont...

Sources:
https://www.python.org/doc/essays/blurb/
https://wiki.python.org/moin/BeginnersGuide
https://swcarpentry.github.io/python-novice-inflammation/06-func/index.html
http://www.bestprogramminglanguagefor.me/why-learn-python
https://www.tutorialspoint.com/python/python_loops.htm

Sort:  

Congratulations @yo-guess! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

The Steem community has lost an epic member! Farewell @woflhart!
SteemitBoard - Witness Update
Do not miss the coming Rocky Mountain Steem Meetup and get a new community badge!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 64916.21
ETH 3483.89
USDT 1.00
SBD 2.45