You are viewing our Forum Archives. To view or take place in current topics click here.
Python - A Programmers Introduction / Basic Tutorial
Posted:
Python - A Programmers Introduction / Basic TutorialPosted:
Status: Offline
Joined: Feb 06, 201014Year Member
Posts: 2,039
Reputation Power: 151
"Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code." ~ Python.org
Python - A Programmers Introduction
This is an introduction to Python. Instead of being for people new to
programming, it is for programmers who want to learn the language as
painlessly as possible.
Python is a Scripting Language. It's homepage is [ Register or Signin to view external links. ]
From there you can download the lastest release of Python.
Running Code
A great thing about Python is the Interpretor. You can type a bit of
code in and it executes it straight away. It is very useful for testing
quick bits of code.
You can use the interpretor, or you can make a file and save it as .py
you can run .py files using the Interpretor. Or on Linux, type the command
python filename.py
Comments
Comments in Python are shown by the hash symbol (#)
Variables
When you declare a variables in Python, it takes an educated guess at
what type it is.
String
Integer
Float
Long
Mulitple assignment
Output
The most simple form of output is the print function.
It displays content to the screen.
It's syntax is: print content
Lists
That makes a new list, lists can contain any variable types, even other lists.
Note that the start with 0.
Operations
You can perform many operations to variable.
Examples:
If statements
Python uses whitespace, it makes code clearer and requires less typing.
For those coming from C++ or the like, Python doesn't have a switch statement
so you must use if statements, which is just as easy.
While loops
While loops are the easiest type of loop in Python.
That would print out the numbers 1 - 10.
For loops
For loops in Python are different to most languages, the best way to see how they
work is to see an example!
Example:
That takes a number as its argument and returns true if it is prime, and false if it isn't.
We call a function like this:
Useful Stuff
Further Reading
If you want to go further with Python, you should read this tutorial:
[ Register or Signin to view external links. ]
Remember that this tutorial doesn't cover much of the Python language,
but it should help you on your way.
Have fun!, sorry if i left anything out or made any mistakes.
It can be called a "scripting language" but is very powerful and has extreme capabilities.
I will be using Python 2.6 for this tutorial.
Onto the basics:
The Official Python Website - [ Register or Signin to view external links. ]
Download the latest version and install on your computer.
Some operating systems may already have it.
I will expect you have some kind of editor of some sort.
Here's a list of them. - [ Register or Signin to view external links. ]
We're going to make a program that asks for a name.
Type this into your editor and save it as hello.py
(.py is a python command line program, .pyw is a non-console application.)
Run the program and you should get the following output:
When you press enter it should close.
Explained:
I'll explain this line by line.
The first line is for Unix/Linux users.
The first line of the program that has that will search usr/bin for python.
Then it will run your program with it.
The second line assigns a variable with the = operator
name is our variable, and it is assigned to raw_input
raw_input is input for strings.
In the () we ask for the person's name. You could even put
name = raw_input('Monkey breath. ')
If you wanted to.
Also, variable names are case sensitive so name and NaMe aren't the same.
The third line prints Hello then prints the variable name.
name is the entry the user entered.
So If I put TTG it would say Hello TTG
The comma after Hello is something used to add another thing and adding a space.
So, print 'hello ', 'world' would print hello world.
The last line is raw_input which gets string input.
It's not assigned to a variable so It just waits for entry then continues on.
And there's nothing else to do, so the program ends.
That's the end of the tutorial.
if interested in Python here's a great tutorial: [ Register or Signin to view external links. ]
I hope you learned something!
Python - A Programmers Introduction
This is an introduction to Python. Instead of being for people new to
programming, it is for programmers who want to learn the language as
painlessly as possible.
Python is a Scripting Language. It's homepage is [ Register or Signin to view external links. ]
From there you can download the lastest release of Python.
Running Code
A great thing about Python is the Interpretor. You can type a bit of
code in and it executes it straight away. It is very useful for testing
quick bits of code.
You can use the interpretor, or you can make a file and save it as .py
you can run .py files using the Interpretor. Or on Linux, type the command
python filename.py
Comments
Comments in Python are shown by the hash symbol (#)
#I am a comment i do not affect the code
Variables
When you declare a variables in Python, it takes an educated guess at
what type it is.
String
my_string = "Hello World!"
Integer
my_int = 12
Float
my_float = 2.3
Long
my_long = 10L
Mulitple assignment
var_1,var_2,var_3=1,2,3
Output
The most simple form of output is the print function.
It displays content to the screen.
It's syntax is: print content
print my_int #prints 12
print mystring, my_float #prints Hello World! 2.3
print "Hello" #prints hello
print 5 #prints 5
print "I like\nNewlines" # \n makes a new line
Lists
my_list = ['hello',1,2,3,'coldDeath']
That makes a new list, lists can contain any variable types, even other lists.
Note that the start with 0.
print my_list #prints ['hello',1,2,3,'coldDeath']
print my_list[4] #prints 'coldDeath'
Operations
You can perform many operations to variable.
Examples:
my_int * 10 #prints 120
my_float + 11.2 #prints 13.5
10 / 5 #prints 2
100 - 54 #prints 46
my_string + "!!!!!" #prints Hello World!!!!!!
If statements
Python uses whitespace, it makes code clearer and requires less typing.
For those coming from C++ or the like, Python doesn't have a switch statement
so you must use if statements, which is just as easy.
if my_int < 15: #True because my_int is 12
print"my_int was less than 15"
elif my_int >= 16: #Else if statement
print"my_int was greater than or equal to 16"
else: #Else statement
print"my_int must be 15"
While loops
While loops are the easiest type of loop in Python.
count = 0 #integer
while count < 10:
print count #print the variable
count += 1 #Count = Count + 1
That would print out the numbers 1 - 10.
For loops
For loops in Python are different to most languages, the best way to see how they
work is to see an example!
Example:
for i in range(1,11): #range (1,11) means 1,2,3,4,5,6,7,8,9,10,11
print i
That takes a number as its argument and returns true if it is prime, and false if it isn't.
We call a function like this:
checkforprime(99)
Useful Stuff
range(1,10) #make a list from 1 to 10
pass #does nothing at all. Used when a statement requires no action
len(variable) #prints the length of the variable in the argument
Further Reading
If you want to go further with Python, you should read this tutorial:
[ Register or Signin to view external links. ]
Remember that this tutorial doesn't cover much of the Python language,
but it should help you on your way.
Have fun!, sorry if i left anything out or made any mistakes.
It can be called a "scripting language" but is very powerful and has extreme capabilities.
I will be using Python 2.6 for this tutorial.
Onto the basics:
The Official Python Website - [ Register or Signin to view external links. ]
Download the latest version and install on your computer.
Some operating systems may already have it.
I will expect you have some kind of editor of some sort.
Here's a list of them. - [ Register or Signin to view external links. ]
We're going to make a program that asks for a name.
Type this into your editor and save it as hello.py
(.py is a python command line program, .pyw is a non-console application.)
#!/usr/bin/env python
name = raw_input('Enter your name: ')
print 'Hello', name
raw_input
Run the program and you should get the following output:
Enter your name: example
Hello example
When you press enter it should close.
Explained:
I'll explain this line by line.
The first line is for Unix/Linux users.
The first line of the program that has that will search usr/bin for python.
Then it will run your program with it.
The second line assigns a variable with the = operator
name is our variable, and it is assigned to raw_input
raw_input is input for strings.
In the () we ask for the person's name. You could even put
name = raw_input('Monkey breath. ')
If you wanted to.
Also, variable names are case sensitive so name and NaMe aren't the same.
The third line prints Hello then prints the variable name.
name is the entry the user entered.
So If I put TTG it would say Hello TTG
The comma after Hello is something used to add another thing and adding a space.
So, print 'hello ', 'world' would print hello world.
The last line is raw_input which gets string input.
It's not assigned to a variable so It just waits for entry then continues on.
And there's nothing else to do, so the program ends.
That's the end of the tutorial.
if interested in Python here's a great tutorial: [ Register or Signin to view external links. ]
I hope you learned something!
Last edited by AoC ; edited 2 times in total
#2. Posted:
Status: Offline
Joined: Sep 27, 201014Year Member
Posts: 241
Reputation Power: 13
Status: Offline
Joined: Sep 27, 201014Year Member
Posts: 241
Reputation Power: 13
Check it out when i go home until then cool!!!!!!!!!
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Jan 09, 201014Year Member
Posts: 1,287
Reputation Power: 57
Status: Offline
Joined: Jan 09, 201014Year Member
Posts: 1,287
Reputation Power: 57
Blew my mind nice post very helpful
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Dec 09, 201014Year Member
Posts: 26
Reputation Power: 1
Status: Offline
Joined: Dec 09, 201014Year Member
Posts: 26
Reputation Power: 1
Nice mate will come in handy soon
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Feb 06, 201014Year Member
Posts: 2,039
Reputation Power: 151
No Problem all, thanks for actually reading.
- 0useful
- 0not useful
#6. Posted:
Status: Offline
Joined: May 15, 201014Year Member
Posts: 2,189
Reputation Power: 325
Status: Offline
Joined: May 15, 201014Year Member
Posts: 2,189
Reputation Power: 325
Don't use Python but if I begin to this will come in handy
Thanks
+Rep
Thanks
+Rep
- 0useful
- 0not useful
#7. Posted:
Status: Offline
Joined: Feb 06, 201014Year Member
Posts: 2,039
Reputation Power: 151
Sorry for the '4' Day Jump. but thanks all.
- 0useful
- 0not useful
#8. Posted:
Status: Offline
Joined: Dec 15, 201014Year Member
Posts: 283
Reputation Power: 13
Status: Offline
Joined: Dec 15, 201014Year Member
Posts: 283
Reputation Power: 13
Excellent post deserves to be stickies +Rep Thanks
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.