Author:TAM, JJ [TAM, JJ]
No description
Tags
Support Statistics
¥.00 ·
0times
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
LEARN PYTHON QUICKLY AND PYTHON CODING EXERCISES CODING FOR BEGINNERS WITH HANDS ON PROJECTS BY J J TAM
Page
3
LEARN PYTHON QUICKLY Install python on MAC OS Python: Hello World program Python interactive command line Python: Operators Arithmetic Operators in python Relational Operators in python Assignment operators in python Logical Operators in python Bitwise Operators in python Membership operators in python Identity operators in python Python: Short circuit operators Strings in python Python: if condition Python: while statement Python: for statement Python: break statement Python: continue statement Python: functions Python: functions: return statement Python lists Python: tuples Python: Sets Python modules Python command line arguments Python: File handling Python: classes and objects Python: Class Vs Instance variables
Page
4
Python: Inheritance Python: Exceptions Python: Handling Exceptions Python global keyword Python: Get type of variable Python Basic – Exercises Get Python version Display current date and time Print the calendar Computes the value of n+nn+nnn Calculate number of days volume of a sphere in Python Compute the area of Triangle Compute the GCD Calculate The LCM Convert feet and inches to centimeters Convert time – seconds Convert seconds to day Calculate BMS Sort three integers Get system time Check a number Python code to Remove first item Filter positive numbers Count the number 4 Find a number even or odd Get n copies of a given string Print out a list which are not present in other list Display details name, age, address in three different lines
Page
5
Program to solve Future value of amount Check whether a file exists Convert the distance Sum all the items Multiplies all the items Get the largest number Get the smallest number Remove duplicates Clone or copy a list Difference between the two lists Generate all permutations Find the second smallest Get unique values Get the frequency of the elements Generate all sublists Find common items Create a list Remove consecutive duplicates Flatten a nested
Page
6
LEARN PYTHON QUICKLY CODING FOR BEGINNERS WITH HANDS ON PROJECTS BY J J TAM
Page
7
LEARN PYTHON QUICKLY Well it is almost 5 years I started my IT career, I love to work in Java, and explore open source packages. Now a day I felt bored to work in Java, so want to learn new language, one of my friend suggested me to learn python. Immediately I thought, why should I learn python? Following are some of the reasons that forced me to learn python…(First and best reason is I am fed up in working with same language for 5 years……….:)) a. Python is open source and is available on Windows, Mac OS X, and Unix operating systems. You can download and work with python for free. b. Python is fun to experiment and easy to learn. Python provides interpreter that can be used interactively, which makes it easy to experiment with features of the language. c. You can extend the python interpreter with new functions and data types implemented in C or C++ d. Python improves developer productivity many times as compared to C, C++ and Java. e. No need to compile python program, it runs immediately after development. f. We can easily port python project from one platform to another (Of course Java also provides this feature) g. Rich in built library. Many third party open source libraries are available for logging, web development (Django — the popular open source web application framework written in Python), networking, database access etc. h. Python has very great community, whatever the problem you faced in python, you will get quick help.
Page
8
Install python on MAC OS Step 1: Download python software from following location. I downloaded pkg file to install on mac os. https://www.python.org/downloads/ Step 2: Click the pkg file. Press Continue. Press Continue.
Page
9
Accept license agreement and press Continue. You can change the installation location use the button ‘Change Install Location’, and press the button Install.
Page
10
Once installation is successful, you will get following screen. Once installation is successful, open terminal and type python3 (Since I installed python 3.5). $ python3 Python 3.5 . 0 (v3. 5.0 :374 f501f4567, Sep 12 2015 , 11 :00 :19 ) [GCC 4.2 . 1 (Apple Inc. build 5666 ) (dot 3 )] on darwin Type "help" , "copyright" , "credits" or "license" for more information. >> > quit() Use ‘quit()’ to exit from python prompt. Location of python3 $ which python3
Page
11
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3 $ which python3.5 /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 On windows machines python usually placed at ‘C:\Python35’.
Page
12
Python: Hello World program Open any text editor and copy the statement “print ('hello world')” and save the file name as hello.py. hello.py print ('hello world') Open terminal (or) command prompt, use the command ‘python3 hello.py’ to run the file hello.py. You will get output like below $ python3 hello.py hello world What happens when you instruct python to run your script? Python first compiles your source code to byte code and sends it to python virtual machine (PVM). Byte code is the platform independent representation of your source code. PVM reads the byte code one by one and execute them. Note: Byte code is not machine understandable code, it is python specific representation.
Page
13
Python interactive command line Open command prompt (or) terminal and type ‘python3’ command. It opens python interactive session. $ python3 Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> How to exit from python interpreter On Windows, a Ctrl-Z gets you out of this session; on Unix, try Ctrl-D instead. Another way is simply call the quit() function to quit from python. $ python3 Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> quit() $ You can type any statement (or) expressions in python interpreter, interpreter execute those statements and gives you the result. >>> 10+20 30 >>> 20*20 400 >>> print('Hello World') Hello World Note: On Unix, the Python 3.x interpreter is not installed by default with
Page
14
name python, so that it does not conflict with other installed Python 2.x executable.
Page
15
Python: Operators An operator is a symbol, which perform an operation. Following are the operators that python supports. Arithmetic Operators Relational Operators Assignment Operators Logical Operators Bitwise Operators Membership Operators Identity Operators
Page
16
Arithmetic Operators in python Following are the arithmetic operators supported by python. Operator Description Example + Addition 2+3 - Subtraction 2-3 * Multiplication 2*3 / Division always returns a float value. To get only integer result use //. 2/3 returns 0.6666666666666666 // Floor division discards the fractional part 2//3 returns 0 % Returns the remainder of the division. 2%3 ** Power 2**3 returns 2 power 3 = 8 >>> 4 + 2 6 >>> 4 - 2 2 >>> 4 / 2 2.0 >>> 4 * 2 8 >>> 4 // 2
Page
17
2 >>> 4 ** 2 16 Relational Operators in python Relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. Operator Description Example == Equal to a==b returns true if a is equal to b, else false != Not equal to a!=b returns true if a is not equal to b, else false. > Greater than a>b returns true, if a is > b, else false. >= Greater than or equal to a>=b returns true, if a is >= b, else false. < Less than a<b returns true, if a is < b, else false. <= Less than or equal to a<=b returns true, if a is <= b, else false. >>> a = 10 >>> b = 12 >>> a = = b False >>> a ! = b True >>> a > b False
Page
18
>>> a > = b False >>> a < b True >>> a < = b True >>> Assignment operators in python Assignment operators are used to assign value to a variable. Following are the assignment operators provided by python. Operator Description = a=10 assigns 10 to variable a += a+=10 is same as a=a+10 -= a-=10 is same as a=a-10 *= a*=10 is same as a=a*10 /= a/=10 is same as a=a/10 //= a//=10 is same as a=a//10 %= a%=10 is same as a=a%10 **= a**=10 is same as a=a**10 >>> a = 10 >>> a 10 >>> >>> a += 10 >>> a 20 >>> >>> a -= 10
Page
19
>>> a 10 >>> >>> a *= 10 >>> a 100 >>> >>> a /= 10 >>> a 10.0 >>> >>> a //= 10 >>> a 1.0 >>> >>> a **= 10 >>> a 1.0 Multiple Assignments You can assign values to multiple variables simultaneously. >>> a, b, c = 1 0 , 'hello ' , 12.345 >>> a 10 >>> b 'hello' >>> c 12.345 Logical Operators in python Following are the logical operators supported by python. Operator Description
Page
20
and ‘a and b’ returns true if both a, b are true. Else false. or ‘a or b’ return false, if both a and b are false, else true. not ‘not a’ Returns True if a is false, true otherwise >>> a = bool( 0 ) >>> b = bool( 1 ) >>> >>> a False >>> b True >>> >>> a an d b False >>> >>> a o r b True >>> >>> no t a True >>> >>> no t (a an d b) True >>> >>> no t ( a o r b) False Bitwise Operators in python Python supports following bitwise operators, to perform bit wise operations on integers. Operator Description
Comments 0
Loading comments...
Reply to Comment
Edit Comment