Python Tips & Tricks
Here I will be sharing a few things around python & its code, I get during my study.
If python is not installed on your machine, then download & install it using below URL, its simple -
www.python.org/downloads/release/python-365/
Or you can pick the version of your choice & download that.
I installed it on Windows10 & the 'PATH' variable is not updated during installation, so we have to update that then.
C:\Users\nitin>python --version
'python' is not recognized as an internal or external command,
operable program or batch file.
===============================================================================================
Check 2 ways to do same thing in Python below -
if name == 'Nitin':
print(f'Welcome {name}')
else:
print('Bye {0}!'.format(name))
If python is not installed on your machine, then download & install it using below URL, its simple -
www.python.org/downloads/release/python-365/
Or you can pick the version of your choice & download that.
I installed it on Windows10 & the 'PATH' variable is not updated during installation, so we have to update that then.
- From the desktop, right-click My Computer and click Properties.
- In the System Properties window, click on the Advanced tab.
- In the Advanced section, click the Environment Variables button.
- Highlight the Path variable in the Systems Variable section and click the Edit button.
- Add the path of your python executable but don't include the name of the executable file, so path will be like - C:\Users\nitin\Anaconda3; .....& not C:\Users\nitin\Anaconda3\python.exe; else you will get below error -
C:\Users\nitin>python --version
'python' is not recognized as an internal or external command,
operable program or batch file.
===============================================================================================
Check 2 ways to do same thing in Python below -
if name == 'Nitin':
print(f'Welcome {name}')
else:
print('Bye {0}!'.format(name))
===============================================================================================
If you have seen Python then you understand that it is an interesting programming language & a very easy one to start with.
I tried Jupyter which you might have seen during my journey with Julia. But I will suggest it to check for some code snippets.
But if you are looking for some IDE to manage your code & project then I will suggest PyCharm(Community version).
===============================================================================================
Below is the code snippet taken from above video & changed a little. It is for tcp connections as show in above videos.
Below code is written on PyCharm IDE -
If you have seen Python then you understand that it is an interesting programming language & a very easy one to start with.
I tried Jupyter which you might have seen during my journey with Julia. But I will suggest it to check for some code snippets.
But if you are looking for some IDE to manage your code & project then I will suggest PyCharm(Community version).
===============================================================================================
Below is the code snippet taken from above video & changed a little. It is for tcp connections as show in above videos.
Below code is written on PyCharm IDE -
tcpServer.py
tcpClient.py
While following above videos, you see example for Sqlite3 & may be it doesn't work for you.
So once you download Sqlite3 & launch CMD, then execute : sqlite3 test.db to create databse with name 'test.db' or change the DB name, to which you are connecting in code. Now try to execute the code, it should work.
Also you can try the below code :
So once you download Sqlite3 & launch CMD, then execute : sqlite3 test.db to create databse with name 'test.db' or change the DB name, to which you are connecting in code. Now try to execute the code, it should work.
Also you can try the below code :
Below code is again from the above videos, but with little change & used PyCharm. So you can check the created serialized file in PyCharm workspace or at the location which you gave in the code. Note : I am dumping Person object first but reading in the last, but Python manages it somehow magically. I don't know how, please let me know it if you know.
===============================================================================================
Prefer to use Tuple over List, as reason shown below, also a way to create a set from the tuple or list -
Prefer to use Tuple over List, as reason shown below, also a way to create a set from the tuple or list -
Output of above code -
If you have got the idea about Python then don't miss below series -
===============================================================================================
Tip : Try to consider below while working with lists or sets
# Use of filter
listNames = ['James', 'Met', 'Alicia', 'Jack', 'Jill', 'James', 'dfds', 'erqwetfrew']
jNames = filter(lambda x:x.startswith('J'), listNames)
print(list(jNames))
# use of reduce
nums = [1,2,3,4,5,6]
result = reduce(lambda x,y:x*y, nums)
print(result)
# use of map
result = map(lambda x:x*2, nums)
print(list(result))
# Creating a list in a single, same way you can populate this list from other list
print([x for x in range(10) if x%2 == 0])
===============================================================================================
If you are trying to find the path where you have installed Python on your Windows system, or may be you have installed
multiple versions of Python, then try the below command to find all the paths of Python installed as shown in below image -
Tip : Try to consider below while working with lists or sets
# Use of filter
listNames = ['James', 'Met', 'Alicia', 'Jack', 'Jill', 'James', 'dfds', 'erqwetfrew']
jNames = filter(lambda x:x.startswith('J'), listNames)
print(list(jNames))
# use of reduce
nums = [1,2,3,4,5,6]
result = reduce(lambda x,y:x*y, nums)
print(result)
# use of map
result = map(lambda x:x*2, nums)
print(list(result))
# Creating a list in a single, same way you can populate this list from other list
print([x for x in range(10) if x%2 == 0])
===============================================================================================
If you are trying to find the path where you have installed Python on your Windows system, or may be you have installed
multiple versions of Python, then try the below command to find all the paths of Python installed as shown in below image -
Now the above command will be helpful to solve the below issue, if you get it while working with PyCharm & trying to install some package like I got below error while trying to install numpy -
Strangely, in PyCharm I was getting above while trying to install numpy package. But using command prompt I was able to
install this package. So I just pointed the python of PyCharm to python installed on my machine like shown below -
Step 1) Ctrl+s to open the settings window shown below & you can see the Project Interpreter being used in PyCharm
currently & this I will change in next window using 'Settings' button shown in top-right corner in below image-
install this package. So I just pointed the python of PyCharm to python installed on my machine like shown below -
Step 1) Ctrl+s to open the settings window shown below & you can see the Project Interpreter being used in PyCharm
currently & this I will change in next window using 'Settings' button shown in top-right corner in below image-
Now in below image we will change PyCharm python to python installed on our system where we have installed our package also.
Step 2) Click on settings button, then select 'Add'. Below window will appear. Then select the shown options & then give the
path of system's Python after clicking those ... (dots).
Step 2) Click on settings button, then select 'Add'. Below window will appear. Then select the shown options & then give the
path of system's Python after clicking those ... (dots).
Then say 'Ok' to all the windows. Then IDE will start mapping discovering the modules in your given python. And now you can use the required package installed on the python of your system.
Utilize the 'Terminal' window of PyCharm
Make use of this feature in PyCharm to interact with your Python code.
Remember Ctrl+L to clear the terminal window
Ctrl+Z and press 'Enter' to exit from your code in terminal window.
To start the interactive session with your Python code, use python -i <your python file name>
Remember Ctrl+L to clear the terminal window
Ctrl+Z and press 'Enter' to exit from your code in terminal window.
To start the interactive session with your Python code, use python -i <your python file name>
Anyone who has watched above videos or already has basic knowledge of Python, still can watch last 2 hours of below video.
If still have no knowledge of Python or new to computer world, then I will strongly recommend to watch the whole video, as you will get some computer basics, information about Python, XML, JSON, Webservices & Databases also.
If still have no knowledge of Python or new to computer world, then I will strongly recommend to watch the whole video, as you will get some computer basics, information about Python, XML, JSON, Webservices & Databases also.