Your Starting Point - Python Tutorial #1

in #steemstem8 years ago (edited)

Hello everyone. This article won't be the usual medical/science stuff. I've never really thought about learning to program because it wasn't much related to my ' area of expertise' but recently I've become really passionate about machine learning and AI. Programming suddenly sparked my interest. I did some research and I've decided to learn Python. By the way, I already know some basic programming principles because at high school we learned Pascal (Really advanced.. I know). I decided to write about everything that I learn. I need to first and foremost disclaim that I in no way claim to be an expert but still it's a really fun way to learn, interact and to teach you guys with me.

This series will include everything that I will learn from almost 0 to over nine thousand (insert vegeta meme here) mistakes and all, also I will include a challenge of same sort at the end of each article to (hopefully) motivate you. With that out of the way let's get started ... (Also of course, I will still write medical articles).


Installing Python


Installing Python seems like a good way to start Python programming. Using my astonishing hacking skills, I googled Python and I went to python.org, downloaded Python 3.6.5 (By the way, I am using windows). The file was about 30 Mb, so it didn't take long to download. Installation was pretty straight forward. Time to choose an interpreter. I did some research and I found that Pycharm was the best. After installing it, I found that it was a bit more complex than I expected and it was a bit slow. I thought it's supposed to work like a "charm" (okay, I will stop...). After some more research, I decided to use Spyder for now. This is what Spyder looks like ...

Also I would like to mention that there are three major versions of Python: the 1. , the 2. and the 3. Any code written in a specific version is guaranteed to work in future versions.

Now it's time to get into the actual language. Let's start with Input/Output.


Input/Output


Any program is supposed to take a user's input and do it's magic to produce a specific output. Here are some of the functions (sequences of statements) you will need:

print() : I bet that this is the most used function in Python because it's used to display a certain information onto the screen. What a better way to try it other than a Hello World program.
Here's what I wrote:

And this is what the console showed:

As you can see I used the print function to display a certain string (I will get into types in a bit). A string is created by adding two single quotation marks or two double marks around a certain word (As you can see from the program, It doesn't matter which one you use). Also, \n is used to escape to a new line.

input() : As you might have guessed, the input function is used to wait for a user's input and returns what they enter to the program. In this example I will be using a variable (which is basically used for storing a certain information to an assigned name) that I will name 'text' (God I am so freaking creative).

Here's what I wrote:

And Here's what I got:

As you can see, the program stored my input (in this case Fancybrothers) inside the text variable and the - inside the sign variable. I didn't use quotation marks because I was referring to a variable. Also, as you can see, you can use the print function to display multiple variables at the same time. You can even display a string with them. If you type print('You entered ',text,' and ',sign,' thus you final text is : ',sign,text,sign) the program will show this: You entered Fancybrothers and - thus your final text is -Fancybrothers-

Next we will talk about Types ...


Types


For now let's talk only about the most basic types of data:

  • Int: Basically an Integer.
  • Float: Numbers that aren't integers like -0.23 or 45.127
  • String: A sequence of characters. These characters could be numbers, letters ...
  • List: A list of variables assigned to a certain index. (By the way, the list index starts at zero)

There is also tuples and dictionaries which I'll save for another post.

Unlike most other languages, In Python, you don't need to specify a variable's type. In the first part of the article, we talked about using a string. There isn't a lot to add except this:

You can add a backslash to use escape characters which are characters that the program may not display properly. For example the ('): print('Hey dude, Steve\' s dad was looking for you').

Floats are created either by entering them directly or by performing a division on integers. Also, be careful when using floats because you can't store them perfectly inside a variable (Decimal fractions with infinite digits).

This is pretty obvious but just in case, you can perform a number of mathematical operations using Python such as addition using a (+) sign, subtraction using a (-) sign, multiplication using an asterisk (*), division using a forward slash (/) and exponentiation using two asterisks (**). You can also use floor division and modulo operators to calculate the quotient or the remainder of a certain division using two forward slashes (//) or a percent symbol (%), respectively. (Division by zero gives an error called ZeroDivisionError)

In Python, you can easily convert one type into another using these functions float, int and str. As you might have guessed float is used to convert an integer or a string into a float, int to convert a string or a float into an integer and str to convert an integer or a float into a string. These conversions can be really tricky to work with because, for instance, you can't store a word inside an integer (Duhh...).

Here is an example where I'll use all of the above...

And Here's what I got:

As you can see, I had to convert Var_N1 into an integer each time I wanted to do a mathematical calculation that's because the input() function turns the user's input into a string. One way to overcome that is to add an int() function to the input line like so : Var_N1 = int(input('Enter an integer: ')). Another way to do so, was to add a simple line where we convert Var_N1 into an integer like so: Var_N1 = int(Var_N1).

Before we talk about the list type. There is also a type called boolean which is basically either True or False (Pretty straightforward if you ask me ...). Here is a quick example:

Attempt 1:

Enter an integer: (I entered 53)
False

Attempt 2:

Enter an integer: (I entered 19)
True

Lets assign it into a variable ...

Enter an integer: (I entered 19)
The result is: True

Let's talk about the List ...

As I said a List is basically a number of variables stored inside a list. You could create a list by using square brackets ([ ]) and using commas (,) to separate all the different items inside the list. Like so:

Todo = ["Wash the car", "Clean the house", "Bury the corpse"]  

You can use the print() function and an index (between square brackets) to display a certain item of the list..

print(Todo[1]) gives Clean the house

You can also use the in operator which returns a 'boolean result'. For example:

True
False

There is also a number of logic operators you can use such as and, or, and not

False
True
False

You can also use methods (I will explain in a later post) on lists such as append which adds an item to the list and remove which deletes an item from the list. There is a lot more but let's focus on those two for now.

To use a method, you have to add a dot and the name of the method next to the variable you want to 'modify'. Like so:

['Wash the car', 'Clean the house', 'Buy Flowers']

The text in grey is called a comment. You could write whatever you want in there and the program won't read it, all you have to do is add a hashtag (#) before your text.

Also, lists could include different types of variables even other lists. I'll explain more in the next post. Here's the challenge for this post: Write a program in which the user is asked to write five numbers, then the program calculates the sum of each two of those five numbers, adds them to a list an then finally displays the list. I will post my solution in the next post. Also, all the programs I wrote can be found in the first comment.

Thank u all, Have a nice day :)

Image Credit:

All the images used above are of my own making...

The Python logo in the thumbnail is from Wikimedia

Useful links for further reading:

Python Data Types

Logical Operators

Spyder

 Things You Need To Know About Spyder 

  One more thing, Join the @steemSTEM community, A community project to promote science technology engineering and mathematics postings on Steemit.     
 

Sort:  

Although have no background in programming or any of these computer languages, however I guess it is still a worthy attempt to try to diversify knowledge and impact others. Those who are drawn to this field will certainly find this post useful.

@eurogee of @euronation and @steemstem communities

Yep, This isn't my specialty really but I found it quite to learn.
Anyway, Thanks for passing by :)

Loading...

thank for guidelines
your post helpfull thanks for sharing post

Congratulations @fancybrothers! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the total payout received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard!


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.081
BTC 61667.55
ETH 1637.39
USDT 1.00
SBD 0.41