Introduction to Python

Prince

[ Verified Seller ]
Staff member
Trusted Seller
Joined
11 yrs. 6 mth. 26 days
Messages
5,381
Reaction score
18,380
Age
45
Wallet
11,590$
Today I'll be doing a basic introduction to programming with Python.
Well briefly go over some of the syntax differences between python and other languages that you may be more familiar with. After that Ill go over Modules which are pythons built in libraries that you can utilise.
Once we have the basics down we'll get into some more tangible stuff like variables, conditionals, and loops. Finially we'll take a brief look at functions and the Python Interactive Interpreter. Grab a cup of coffee cause this might be a long tutorial.  Also note this tutorial is based on the Python 2.x branch, particularily 2.7. If you code in 3.x and have any questions feel free to ask and Ill get an answer back to you as soon as I can.
This isn't an overly thorough tutorial, python is an awesome language and as a result its very extensive, i've tried to cover most of the basics here so that by the time you're finished you can sit down and start writing some code, if you have any questions feel free to contact me and stackoverflow is a godsend.
Common Phrases:
cast - changing a variable from one data type to another
class - an abstract data structure
module - another python script that can add extended functionality
object - an abstract data structure, an object is an instance of a class

Python Syntax:
The syntax in python is different from what you might be familiar with in languages like PHP or C++. Lines do not need to be terminated by a semi-colon in python(although you can still use them and it will execute fine, the standard convention is to omit them) There are also no curly braces in use in python to denote a block of code. As such code NEEDS to be indented properly in python or it will not run.  
Indentation needs to be consistent as well, you can for example use 4 spaces as an indent or one tab but you can not interchangeably use them both throughout the same program(as a rule of thumb when i copy python code i immediately do a find and replace of 4 spaces to \t to make life easier, if you port code from windows to python you may find you need to use dos2unix or unix2dos for the vise versa)
If you are familiar with how indentation works then this shouldn't be a problem, if you aren't aware what I'm referring to we'll get to that shortly.Python is also distinct from other languages in the amount of english used in its syntax, where other languages use the || and && logical 'or' and 'and' operators respectively in python you can simply write the words: 'or' and 'and' in conditional statements. This english based syntax mixed with strongly enforced indentation makes python code quite easy to read at initial glance which is one of the many benefits of Python.(there is also a keyword 'not' in place of ! however you can still use ! as an operator as in the example: 1 != 2)

Modules:

If you're wondering if you can do something in python the answer is almost invariably yes, there is almost certainly already a module in existance that will make your life easier.  Python comes pre-packaged with a ton of useful modules of all types that you can use and theres just as many or more third party modules available that usually require only a simple install script be run to make them accessible. To import a module in essence the same as including a header file in C/C++ or doing an include/require in php or whatever the equivalency is in any other language, same deal.
The syntax to do so is:
import modulename
theres also a few spins on this, you can import multiple modules in one line like:
import module1, module2, module3
or you can choose to import only a sepcific class or method from a module
from module1 import class3
if you use import modulename you will find often that you have to declare modulename.classname() when you instantiate an instance, I forget the exact reasons but it has to do with how it is imported into the namespace, to avoid this you can write:from module1 import *which will import everything into the global namespace, be warned though this can be exhaustive on memory and a bad idea for several reasons, its not against standard procedures to do it but dont use it in a sloppy manner be sure theres a reason for it.you can also import other scripts you have written, if they are in the same cwd as the script you are executing you can simply typeimport myscriptif your script is named 'myscript.py' it will now be imported, besure to omit the .py in the import call.you can also import local files in a subdirectory provided you use:import folder.myscript
 
Paid adv. expire in 2 months
CLICK to buy Advertisement !
westernunion carding Verified & Trusted WesternUnion | MoneyGram | Bank - Transferring [299$ BTC for 2000$ WU]
electronics carding Verified & Trusted Electronics Carding, Carding iPhone, Samsung Carding, MacBook Carding, Laptops Carding

tototexas

Well-known member
Member
Joined
10 yrs. 3 mth. 17 days
Messages
1,281
Reaction score
21
Wallet
0$
very nice thank you...
is there some book or some specific way to follow to learn?
 

ClarenceCTZ

Active member
Member
Joined
1 yrs. 9 mth. 29 days
Messages
29
Reaction score
1
Wallet
0$
I see you are sharing information about Python syntax and how to import modules in Python. It's great that you are providing these useful details to others. Keep it up and keep sharing your knowledge on the forum.

Any methodology you use while programming?
Greetings broth
 
Top Bottom