Sie sind auf Seite 1von 6

Python Begins THURSDAY, SEPTEMBER 15, 2011 Collections Before discussiing about the Collections in Python ,we all

must know the answER of one very important question ,ie; What exactly we can do with a Collection? well there are a some basic operations which normally perform using collections : 1. Add objects to the collection. 2. Remove objects from the collection. 3. Find out if an object (or group of objects) is in the collection. 4. Retrieve an object from the collection (without removing it). 5. Iterate through the collection, looking at each element (object) one after another. So without wasting any time let us directly discuss the types of Collection avai lable in Python: 1. List 2. Tuple 3. Dictionary or Hash 4. Array or Vector 5. Stack 6. Bag 7. Set There are several merits and demerits of using different Collections which we wi ll be discussing in our next session with programs.. Have a good day...! Posted by Narendra Soni at 6:17 AM 0 comments Email This BlogThis! Share to Twitter Share to Facebook WEDNESDAY, SEPTEMBER 14, 2011 Boolean Operators After dealing with String and Arithematic operators ,its time to cover some very important operators , Boolean Operators Boolean values are sometimes known as "truth values" because they are used to te st whether something is true or not. example we are having two Strings we want t o know weather the String values provided are equal or not, if they are equal we transfer pur control from one location to another. so like in all other programming languages, in Python also we are having all bas ic Boolean operators:: [A and B] ----> AND ---> True if A,B are both True, False otherwise. [A or B] ----> OR ---> True if either or both of A,B are true. False if both A a nd B are false [A == B] ----> Equality ---> True if A is equal to B [A != B A <> B] ----> Inequality --->True if A is NOT equal to B.

[not B] ----> Negation ---> True if B is not True Note: the last one operates on a single value, the others all compare two values . Posted by Narendra Soni at 9:43 PM 0 comments Email This BlogThis! Share to Twitter Share to Facebook Working with Strings In Python, strings can be represented in several ways: With single quotes: 'Here is a string' With double quotes: "Here is a very similar string" With triple double quotes: """ Here is a very long string that can if we wish span several lines and Python will preserve the lines as we type them...""" One special use of the latter form is to build in documentation for Python funct ions that we create ourselves - we'll see this later. We can access the individual characters in a string by treating it as an array o f characters. There are also usually some operations provided by the programming language to help us manipulate strings - find a sub string, join two strings, copy one to another etc. String operators Operator Description S1 + S2 Concatenation of S1 and S2 S1 * N N repetitions of S1 now lets create an example using the above mentioned string operators: StringOperators.py ------------------------s1=' again' s2= ' and again' print (s1+ s2 ) # string concatenation print (s1 * 3) # string repetition print(' Again ' + (' and again' * 3) ) print(s1+(s2*3)) output: again and again again again again Again and again and again and again again and again and again and again Notice that the last two examples produced the same output. Posted by Narendra Soni at 8:58 PM 0 comments Email This BlogThis!

Share to Twitter Share to Facebook Modulo and Exponential arithematic operation In last session i left out the two most important arithematic operators Modulo a nd the Exponential operator M % N Modulo: find the remainder of M divided by N M**N Exponentiation: M to the power N so now we ill be developing a python program ModuloExponential.py which covers s ame. so without wasting anymore time ,right click one the project icon ---> new --> P yDev module---> enter the name of module as ModuloExponential and click on OK.. now we are ready ModuloExponential.py ------------------i1=3 # create an integer and assign it to i1 i2= 4 # create an integer and assign it to i1 i3 = i1**i2 # assign the result of 3 to the power 4 to i3 print(i3) i4= i2 % i1 # assign the result of 4 mod 3 to i4 print(i4) Output: 81 1 Was very simple program,hope u all got it easily.. now letz discuss some shortcut operators Operator Example Description M += N M = M + N M -= N M = M - N M *= N M = M * N M /= N M = M / N M %= N M = M % N Posted by Narendra Soni at 8:51 AM 0 comments Email This BlogThis! Share to Twitter Share to Facebook Diving into Python Learning different programming languages ,technologies is like a passion for me and same i am doing this with Python as well ,thanks to Tata Consultancy Service s for making us dive in into Python straight away.. As i know except few most of you are from other streams , so considering yoursel

f as Beginners itz better to start with very basics of Python. The program which we are discussing now covers Arithematic operators and printin g the complex calculated results in desired form.. Arithematic Operators we are available with are: ---------------------------------------------------------------a) add(+) b) subtract (-) c) multiply (*) d) divide (/) let say the the name of our new python project is "PythonArithematic" , let me r epeat how we can create this project in MyEclipse 8.6, hope you are all done wit h integration of MyEclipse and PyDev plugin...if not please go throgh previous posts.. Creating PythonArithematic project:: -------------------------------------------------------------------1. Go to File --> New ---> PyDev Project to start a wizard. 2. In the next window that appears, enter the name of project as "PythonArithema ti" and select "python" and 3.0"; as the type. Make sure "create default 'src' f older and add it to the pythonpath?" is selected. Click Finish. 3. Create a new module :: Select the project "PythonArithemati" which we just created and go to File ---> New ---> PyDev Module. This will launch a new PyDev Module Wizard where we shoul d enter a name for your module as "Arithematic.py" . Leave the Package field bla nk and select Finish. 4. The file should be opened in the open space in the center of the workspace-th e Editor view. (If not, right click on the Arithematic.py icon and select Open.) You will see a tab with the name of your file. so ..wat next.... you are now availabe with everything ready ...letzz do some ar ithematic operations.. Arithematic.py -----------------a=2 b=3 c=a+b print(c) #addition c= a-b print(c) #subtraction c=a*b print(c) #multiplication c= b/a #division print(c) #printing the resuls in desired format print("the sum of %d and %d is %d" %(a,b,a+b)) Output: ---------

5 -1 6 1.5 the sum of 2 and 3 is 5

So we finished up performing addition,subtraction,multiplication and division op eration. >>>print("the sum of %d and %d is %d" %(a,b,a+b)) In this command the format string contains '%' markers within it. The letter 'd' after the % tells Python that a 'decimal number' should be placed there. The values to fill in the markers are obtained from the values inside the bracke ted expression following the % sign on its own. There are other letters that can be placed after the % markers. Some of these in clude: %s - for string %x - for hexadecimal number %0.2f - for a real number with a maximum of 2 decimal places %04d - pad the number out to 4 digits with 0's The Python documentation will give lots more...I will suggest everyone to please go through Python documentaion . Posted by Narendra Soni at 12:33 AM 0 comments Email This BlogThis! Share to Twitter Share to Facebook TUESDAY, SEPTEMBER 13, 2011 HelloWorld.py Posted by Narendra Soni at 10:58 PM 0 comments Email This BlogThis! Share to Twitter Share to Facebook Writing Fist Python program Like all tutorials we too gonna start with HelloWorld.py Like in Java we save files with ".java" as extension ,Python files are saved wit h .py as extension. For writing Python we need to switch our perspective from java to PyDev ,we can do this by: Go to Window ? Open Perspective ? Other and choose PyDev, then click OK. If you look at the upper right corner you will see that the perspective has changed fro m "Java" to "PyDev".

Creating PythonTest project:: --------------------------------------------------------------------

1. Go to File ? New ? PyDev Project to start a wizard. 2. In the next window that appears, enter the name of project as "PythonTest" an d select "python" and 3.0"; as the type. Make sure "create default 'src' folder and add it to the pythonpath?" is selected. Click Finish. 3. Create a new module :: Select the project "PythonTest" which we just created and go to File ? New ? PyD ev Module. This will launch a new PyDev Module Wizard where we should enter a na me for your module as "HelloWorld" . Leave the Package field blank and select Fi nish. 4. The file should be opened in the open space in the center of the workspace-th e Editor view. (If not, right click on the HelloWorld.py icon and select Open.) You will see a tab with the name of your file. 5. lets Write and run the program :: Simply type print('Hello, World!') into the file. You may remove the default doc comment or leave it there; Python ignores it. press ctrl+s or Right click on the file and select Save to save HelloWorld.py. Finally, choose the HelloWorld.py icon, and go to Run ? Run As ? Python Run to r un our program. (A quicker alternative is to right-click on the HelloWorld.py icon, and select R un As ? Python Run, or press Ctrl+F11.) Everyone have a Look at the bottom of your screen at the Console view and you wi ll see the message you told the computer to print.ie; Hello, World!

Das könnte Ihnen auch gefallen