Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 2 votes

{1} [Tutorial] Beginner's Guide to Python

Tutorial notorius python tutorial beginner

  • Please log in to reply
5 replies to this topic

Poll: Python Tutorial Survey (2 member(s) have cast votes)

Was This A Useful Python Tutorial?

  1. Yes (1 votes [50.00%])

    Percentage of vote: 50.00%

  2. No (0 votes [0.00%])

    Percentage of vote: 0.00%

  3. See My Comments Below (1 votes [50.00%])

    Percentage of vote: 50.00%

Vote Guests cannot vote

#1 Notorius

Notorius

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Location:USA
  • Programming Language:Python, ActionScript, Others
  • Learning:C, Java, C++, Objective-C, C#, JavaScript

Posted 18 August 2012 - 12:33 PM

I'll be working on tutorials on Python for the general audience - targeted specifically for the new learner.

[-------------------------------------------------------[]-------------------------------------------------------]

Contents

1. A short introduction

2. The Beginning of the World (of Python)

3. How do I get Python?

4. What do I do now?


[-------------------------------------------------------[]-------------------------------------------------------]


A Short Introduction:

So what IS Python? Before you read the next excerpt from Python.org, please be aware that I'll have a highly summarized passage following directly after... In short, tl;dr is at the end of this section :)


So, according to Python.org,

"Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.
Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn't catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective."

TL;DR(1)

Python is a flexible programming language that is object-oriented and regarded as a high-level programming language. However, the syntax and functions are extremely simple and easy to learn, thus making it a great first language to learn. Python also has the ability to create modules (will be discussed later), which allows for code reuse. It does not require compilation, which is why the language is fast-paced and development friendly.


TL;DR(2)

Python is Awesome.


[-------------------------------------------------------[]-------------------------------------------------------]


The Beginning of the World (of Python):

So you may be wondering what Python is used for exactly (that's the question that plagued my mind when I first started learning). Well, before I scare you away by presenting a wall of text, here's a nice simple bulleted list:

  • Creating Python Games
  • Web Development
  • GUI's
  • Calculations
  • Software
  • Network Programming
  • Database Usage

(This list was gathered from Python.org)


In this tutorial series, I will mainly be focusing on basic software, games, and calculations.



[-------------------------------------------------------[]-------------------------------------------------------]


So... How Do I Get Python?:

HOPEFULLY, you've already got Python. BUT, sorry to annoy you with my own preferences, I teach with Python 3.2.2.

If you're feeling lethargic, here's a link to it:

http://www.python.or...releases/3.2.2/

Once you download that, you'll see Python Command Line. You don't want to use this to edit code. Bring up your search bar and look up "IDLE (Python GUI)". Once you've found that, drag it to somewhere memorable - we'll be using that a lot.

[-------------------------------------------------------[]-------------------------------------------------------]


What Do I Do Now?:

Now, I don't want to leave you guys with nothing but a meaningless shell for a programming language, so I'll stick in a bit of ACTUAL programming in this tutorial.


[font="lucida sans unicode, , , lucida grande, , sans-serif}"]Probably the first thing that any programmer learns first is Math Operations.[/font]

(What's up with Math, anyways?)


SO.

Math is quite simple is Python.

First, let's open up IDLE and open up a new file. (On windows, the shortcut is ctrl - N, on Macs the shortcut is Apple - N. Or you can press "File > New Window".


IMPORTANT: The following code may not be reproduced, sold, or used for any other

applications than for personal use, unless permission is granted by the author himself.


Copy this into your window:

#Informative Property of Jonah Chang. All rights reserved.
#The following code may not be reproduced, sold, or used for any other
#applications than for personal use, unless permission is granted by the author himself.

try:
print("Prelude: Math Operators")
input("Press enter to continue...")
print()
print("Math operators are the simplest Python functions available.")
print("They allow you to do... MATH.")
print("Here are the following operators used in math!")
input("\nPress enter to show the list...")
print("_________________________")
print("|a+b - Addition |")
print("|a-b - Subtraction |")
print("|a*b - Multiplication |")
print("|a/b - Division |")
print("|a//b - Floor Division |")
print("|-a - Negatives |")
print("|abs(a)- Absolute Value |")
print("|a**b - Exponent (a^<img src='http://cdn.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' /> |")
print("-------------------------")
print()
input("Press enter once you're done reading!")
print()
print("Alright, well let's try out some math functions!")
print("Entering the python shell...\n")
input(">>> Hey there! Let's start with some simple addition.")
print(">>> First, let's add 2 + 2. Adding spaces in between helps")
print(">>> keep the code organized, but it doesn't really matter.")
print(">>> For now, we'll add spaces.")
print(">>>")
input(">>> Press enter, then type in 2 + 2.")
while True:
matha = input(">>> ")
if matha == "2 + 2":
print("Awesome! That's it for the prelude")
break
elif matha == "2 + 2 ":
print("Awesome! That's it for the prelude")
break
elif matha == " 2 + 2":
print("Awesome! That's it for the prelude")
break
else:
print(">>> Sorry, try that again. Did you add spaces?")
print(">>> Exiting Python Shell...\n")
print("Great job! Isn't addition in python easy?")
print("The rest of the math operators work the same way.")
print("Feel free to work on your own calculations with the Python Shell!")
except ValueError:
print("Integers only! <img src='http://cdn.codecall.net/public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />")




Once done, save it as "Prelude.py". Keep the window open. Now press "Run> Python Shell".


If you're not sure where that is, hit F5.


That's it for this first tutorial!


  • 0

#2 WaterNode

WaterNode

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 69 posts
  • Location:Seattle, WA, United States of America
  • Programming Language:C, Java, C++, C#, Python
  • Learning:C, Java, C++, C#, Python, Lua, Assembly, Others

Posted 23 August 2012 - 09:14 PM

I believe |a| works also instead of the confusing abs(a).
  • 0

#3 Notorius

Notorius

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Location:USA
  • Programming Language:Python, ActionScript, Others
  • Learning:C, Java, C++, Objective-C, C#, JavaScript

Posted 24 August 2012 - 08:20 AM

Thanks for your input; I'll add it in!


--EDIT--

I tried it in Python shell, but it didn't seem to work for me; What version of Python are you running?
  • 0

#4 WaterNode

WaterNode

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 69 posts
  • Location:Seattle, WA, United States of America
  • Programming Language:C, Java, C++, C#, Python
  • Learning:C, Java, C++, C#, Python, Lua, Assembly, Others

Posted 24 August 2012 - 10:35 AM

3.2.3 of course. Do you happen to be from Minecraft Forums or HF? Because recognize your username.
  • 0

#5 Notorius

Notorius

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Location:USA
  • Programming Language:Python, ActionScript, Others
  • Learning:C, Java, C++, Objective-C, C#, JavaScript

Posted 24 August 2012 - 11:03 AM

Hm, interesting. It doesn't seem to work for me. And yes, I am on both of those forums under the same alias (also Pyran/PyranDev).

  • 0

#6 WaterNode

WaterNode

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 69 posts
  • Location:Seattle, WA, United States of America
  • Programming Language:C, Java, C++, C#, Python
  • Learning:C, Java, C++, C#, Python, Lua, Assembly, Others

Posted 24 August 2012 - 11:43 AM

Agreed, interesting. I remember you commenting on one of my programming threads, but I decided to move here instead.
  • 0