DEVTOME.COM HOSTING COSTS HAVE BEGUN TO EXCEED 115$ MONTHLY. THE ADMINISTRATION IS NO LONGER ABLE TO HANDLE THE COST WITHOUT ASSISTANCE DUE TO THE RISING COST. THIS HAS BEEN OCCURRING FOR ALMOST A YEAR, BUT WE HAVE BEEN HANDLING IT FROM OUR OWN POCKETS. HOWEVER, WITH LITERALLY NO DONATIONS FOR THE PAST 2+ YEARS IT HAS DEPLETED THE BUDGET IN SHORT ORDER WITH THE INCREASE IN ACTIVITY ON THE SITE IN THE PAST 6 MONTHS. OUR CPU USAGE HAS BECOME TOO HIGH TO REMAIN ON A REASONABLE COSTING PLAN THAT WE COULD MAINTAIN. IF YOU WOULD LIKE TO SUPPORT THE DEVTOME PROJECT AND KEEP THE SITE UP/ALIVE PLEASE DONATE (EVEN IF ITS A SATOSHI) TO OUR DEVCOIN 1M4PCuMXvpWX6LHPkBEf3LJ2z1boZv4EQa OR OUR BTC WALLET 16eqEcqfw4zHUh2znvMcmRzGVwCn7CJLxR TO ALLOW US TO AFFORD THE HOSTING.

THE DEVCOIN AND DEVTOME PROJECTS ARE BOTH VERY IMPORTANT TO THE COMMUNITY. PLEASE CONTRIBUTE TO ITS FURTHER SUCCESS FOR ANOTHER 5 OR MORE YEARS!

Python Programming Tutorials

Introduction

Hello, my name is TheDischarger, yes an alias, but we’ll use that anyway. Now, i’m going to assume that you are new to this whole programming and coding biz (which are both the same) so don’t expect anything very useful if you have previous experience with coding beforehand. So with that introduction, lets get started.

Installing and using your software

You want to start coding in Python? You can’t just simply ‘jump’ into it, it takes time and dedication like any other activity. Before we do anything though, we’ll need the compiler that enables us to actually type in and run the code. Head over to the official website and download the newest version of Python. After that, you should be able to compile and run Python Scripts (.py extensions on windows). Python doesn’t really have its own environment, unlike popular languages like Java and C (Eclipse and DevCPP), instead, the user relies on Python to compile .py scripts and in order to create/edit these scripts, the user needs to open them in a text editor of choice. After that, you should be able to open the script and Python can automatically run it for you. Please note that there are alternative compilers that can do this for you (Like the site Compile Online). (Please Also note that this tutorial will follow Python 3.x.x’s syntax, and 2.x.x and future versions may be different).

Hello World!

Python is a high level language, so essentially there really is no extra overly-long lines to type in (*cough*Java*cough), so in order to pr‘Hello World!’ to our output is by typing this:

#!/usr/local/bin/python3

pr("Hello World!\n");

Wow, that was short, eh? Lets get to disassembling and explaining each parts of the code, shall we:

  • ‘#!/user/local/bin/python3’ is just a clarification to let python know which version of python you’re using, you should use this if you have multiple versions of Python, or your installation is in another place outside of the default.
  • ‘print’ prints whatevers in the argument (parenthesis) into the output.
  • ‘(“Hello World!\n”);’
    • The parenthesis are arguments, which you can look at as settings to a piece of code.
    • The quotes “” tells Python that you want it to display a string (Raw Text) into the output.
    • the ‘\n’ which you have to put inside the quotes, tells Python to create a new line (Python will never prthis).
    • the semicolon is used to declare the ending of a line of code.

If you’ve read my previous programming tutorial Java, you would find that this is much simplistic. This is all we need to know to output basic text into the output, until then, we’ll see you for the next round!

Lets do some math

Like i’ve said, Python is a high level language, so there aren’t really long lines to type. Essentially, just like the ‘print’ line above, this is also very short:

result = 2 + 2
print(result);

Ok, so what does this do exactly? You can probably guess already from the few parts of this, but i’ll explain.

  • ‘result’ in the same of the variable.
  • = tells Python that the variable is assigned to that number/string.
  • 2 + 2 is simply an addition operator, it says for itself. This is also what the value ‘result’ is.
  • We put the variable ‘result’ in the argument (Parenthesis after println, you’ll get used to these technical terms), note that there are no quotes since we are assigning an integer, not a string.

If we did it correctly, you should be able to get the number 4 in the console. But what if we don’t want to add? No problem, subtraction is here, as a matter in fact, you can do all sorts of operations:
Subtraction:

result = 2 - 2

Multiplication:

result = 2 * 2

Division:

result = 2 / 2

Modulo (Divides and gives you the remainder):

result = 3 % 2

Alright, so we now know the most basic math functions. With that said, i’ll see you in the next section! Good bye.

Variable Types

Like all languages, there are integers, strings, floating points, but what does Python have? We all know by now that you don’t need to specify which type of variable type to use for a variable. But here are the variable types you’ll need to know:

  • Integers are a simple whole number. It can span up to a maximum number of 9223372036854775807.
  • Floating Points are simply variables that can hold decimals.
  • A string is a variable that can hold text or digits into a form of raw text (When assigning, they always must have quotes “”).
  • A list is very much like an array (Don’t know what that means? I’ll tell you right now). An list is capable of holding multiple values in a single variable, useful if you don’t want to create too much objects(Variables).
  • A Tuple is very much like a list(Array), except that they are inclosed in parentheses, and cannot be changed (Read-Only). This is useful if you want to make a constant list.
  • A Dictionary is like a Hash Table type (Think of it like sorting a bunch of people randomly scattered around with their friends into groups). This type uses the computer science term ‘Associative Array’ so it is similar to a list.

Of course, in some cases you’re going to have to convert variable types (For example: inputting a string number with a scanner, and converting it into a number for calculations), so I have this table for you that you may find very helpful.

int(x [,base])
Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )
Converts x to a long integer. base specifies the base if x is a string.
float(x)
Converts x to a floating-point number.
complex(real [,imag])
Creates a complex number.
str(x)
Converts object x to a string representation.
repr(x)
Converts object x to an expression string.
eval(str)
Evaluates a string and returns an object.
tuple(s)
Converts s to a tuple.
list(s)
Converts s to a list.
set(s)
Converts s to a set.
dict(d)
Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s)
Converts s to a frozen set.
chr(x)
Converts an integer to a character.
unichr(x)
Converts an integer to a Unicode character.
ord(x)
Converts a single character to its integer value.
hex(x)
Converts an integer to a hexadecimal string.
oct(x)
Converts an integer to an octal string.

And you’re set. Whew, variables are quite a lot aren’t they? Just like they are, we use variables in everything. From written algebra to a mainframe computer. It’s quite important to know what every single variable is, so that the next time you want to type in a bajillion numbers at once, you know to use either a short, a int or a long. Until then, see me at the next chapter, and we’ll do some serious coding.

Our first program

With our newly gained knowledge of math operators, i’d say it’s finally time to get into some serious business. What I’m going to show you is a simple interest calculator that outputs the money after interest.

principle = 10000
rate = 0.07
time = 36
time = time/12
result = principle * rate * time
result = principle + result
print(result);

You’re result is probably 12100. So wow, ok, so you’re probably thinking “Is this all….” well, pretty much it. If you do a side comparison with java and python, you’ll find yourself saying that Java is a nightmare when it comes to typing, and you’re probably right. Python is a high level programming language, so essentially, the API and the Compiler itself will determine what is ‘what is what’ and can classify code ‘smarter’. So arere you getting the hang of it? I hope so cause we’re about to do something even more advanced in the next section. Ciao!

Programming


QR Code
QR Code python_programming_tutorials (generated for current page)
 

Advertise with Anonymous Ads