Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Python,Part1
Index -> Programming, Python -> Python Tutorials
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SS_198




PostPosted: Tue Jun 16, 2009 1:58 pm   Post subject: Python,Part1

This isn' so much of a tutorial as a basic command structure for python.

I hope this little peice of information helps you out, i've been reading some python books and this is a bit of what i've learned so far using python.



How To Run Python Programs Via Cmd prompt
C:\program.pyc



Begin Python:

The first program we will learn the the "Hello World" program, basicly an output program.
There are 3 ways to do so, each with there benifits but ill edit more later to explain better.

Program1:
code:

>>> print ("Hello, World")
<Hello, World #Output


Program2:
code:

>>> x = "Hello, World"
>>> x
<Hello, World # Output


Program3:
code:

>>>print ("Hello" + "World")
<Hello World



Now let's say you want to ask a question, so what you type is the following
Program4:
code:

>>>p4 = input() #user will input a varible into p4
34                  #i entered 34 but it waits for an input
>>>p4
<34



A few things to sort out:

# Means that everything after # will be ignored




Okay now since we've writen our first programs let's move onto some more difficult stuff.

Dictionarys

Well To Store mass amount of information you can use dictionaries.
A dictionary file looks somewhat like this in python > 3.0.1

Program6: Dictionaries.
"Shopping List"

code:

>>>p6 = {"Cookies":100, "Bread Stick": 5 , "Bags of Milk" : 2} # you #can keep adding stuff if u want just make sure Words are in #"qoutations".
#after the quotiontions you need to type : then the amount of stuff you #wish to buy(the number).

#Now To Delete an item from your list type the following.


>>>p6.pop("cookies")
<5
>>>p6
<{"Bread Stick":5, "Bags of Milk": 2}


#now to add multiple items at once.
>>>p6additems = {"Butter":2, "Cheese" : 3}
>>>p6.update(p6additems)
<{ Butter": 2, "Cheese" : 3 "Bread Stick": 5 , "Bags of Milk" : 2}



Program7:
Loop + if statement
code:

#This is a simple program, a number guessing game.
#There will be a built in function that will tell you
#,if you are close to the number that you have typed.

#Start Off with a secret number.

ui = input("Enter a Random Number: ") #ui = user input
c = True # True or False Statement
while c: #if c = True then the loop will begin, if false the loop stops

        x = input("Enter a Number") # Gets Users input
        if x < ui: #checks if x is less than ui
                print ("The Number is To Small, Try Again")
        elif x> ui: #checks if x is greater than ui
                print ("The Number is to big, try again")
        elif x==ui # checks if x is equal to ui
                print ("You Got the Number!")
                c = False #closes the loop


Program8:

code:

ui = input("Enter a Random Number: ") #ui = user input
c = True # True or False Statement
while c: #if c = True then the loop will begin, if false the loop stops

        x = input("Enter a Number") # Gets Users input
        if x < ui: #checks if x is less than ui
                print ("The Number is To Small, Try Again")
        elif x> ui: #checks if x is greater than ui
                print ("The Number is to big, try again")
        elif x==ui # checks if x is equal to ui
                print ("You Got the Number!")
                break # closes the loop

Sponsor
Sponsor
Sponsor
sponsor
Zeroth




PostPosted: Sat Jun 20, 2009 10:51 am   Post subject: Re: Python,Part1

A few nitpicky details by the resident compsci.ca python guru(one of them anyways...)...

You need to specify that this is python v3. In previous versions of python, print was not callable. As well, your use of input only gives strings, not numbers, in v3. You need to cast it, using the factory function int. I think you should introduce lists first, before dictionaries. Plus, you are doing a lot of list-specific functionality on dictionaries, like pop, push, update. I know they work, but the conceptual ideas should be introduced with the lists. For your dictionary program, you should use only dictionary-style methods, ie, has_key, dict[key] access, keys(), values(), etc. You did misspell dictionaries.
SS_198




PostPosted: Sat Jun 20, 2009 11:20 am   Post subject: Re: Python,Part1

Zeroth @ Sat Jun 20, 2009 10:51 am wrote:
A few nitpicky details by the resident compsci.ca python guru(one of them anyways...)...

You need to specify that this is python v3. In previous versions of python, print was not callable. As well, your use of input only gives strings, not numbers, in v3. You need to cast it, using the factory function int. I think you should introduce lists first, before dictionaries. Plus, you are doing a lot of list-specific functionality on dictionaries, like pop, push, update. I know they work, but the conceptual ideas should be introduced with the lists. For your dictionary program, you should use only dictionary-style methods, ie, has_key, dict[key] access, keys(), values(), etc. You did misspell dictionaries.



Ya, i guess your right, but im mostly editing and adding when i have time and after i've tested each program Smile

> i was thinking about adding lists before dictionaries but i was a little lazy.
>but ill be adding more information after i've edit my python notes Smile
the first post was very messy but ill add more once im finished reading // reviewing : books and notes, ill make more edits. Smile

thanks for input though.
Display posts from previous:   
   Index -> Programming, Python -> Python Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: