Useful Python Programming Tips

In this post, I will be regularly posting useful Python programming tips.

These tips will be organized in different categories, in order to make it easier to read it.

 

Hello World in Python

The below command, prints a “Hello World” message from within a Python script:

print("Hello World from Python")

 

Code Indentation in Python

In many programming languages, code indentation plays only a formatting role, however, in Python, code indentation is used for setting code blocks. Check for example the below if…else statement:

#if...elif...else example
a=200
b=200
if a>b:
    print("a is greater than b")
elif a==b:
    print("a and b are equal")
else:
    print("b is greater than a")

As you can see, within each if, elif and else code blocks, I include one command in right indent.

This is an example of how code indentation is used in Python.

 

How to use Visual Studio Code for Python Development

Among the many IDE options you have for programming in Python, you can also use Visual Studio Code, which is a free IDE by Microsoft that supports developing in many programming languages.

So, after you have installed Visual Studio Code, you can find a free Python extension from its marketplace, install the extension and that’s it, you are ready to start developing in Python. In my setup, when developing in Python within Visual Studio Code, I use Microsoft’s Python extension.

In order of course to be able to run a Python script, you also need to install Python in your environment. You can find the available Python installers here.

 

Comments in Python

To comment a line of code in Python, you just need to use the # sign. Here’s an example:

#This is comment in Python

 

How to Easily Split a String in Python

In Python, you can split a string very easily. Check the below example:

string="Programming in Python"
splitString=string.split()

print(splitString)
print(splitString[0])
print(splitString[1])
print(splitString[2])

 

More Python programming tips are coming soon!

 


Learn More about Python Programming and SQL Server Data Access

Enroll to our online course  “Working with Python on Windows and SQL Server Databases” with an exclusive discount, and get started with Python data access programming for SQL Server databases, fast and easy!

Working with Python on Windows and SQL Server Databases - Online Course
(Lifetime Access, Q&A, Certificate of Completion, downloadable resources and more!)

Enroll from $12.99


Get Started with Programming Fast and Easy – Enroll to the Online Course!

Check our online course “Introduction to Computer Programming for Beginners(special limited-time discount included in link).

Learn the philosophy and main principles of Computer Programming and get introduced to C, C++, C#, Python, Java and SQL.

The Philosophy and Fundamentals of Computer Programming - Online Course - Get Started with C, C++, C#, Java, SQL and Python
(Lifetime Access, Q&A, Certificate of Completion, downloadable resources and more!)

Enroll Now with Discount!


 

Featured Online Courses:

 

Read Also:

 

Subscribe to our newsletter and stay up to date!

Subscribe to our YouTube channel (SQLNetHub TV)

Easily generate snippets with Snippets Generator!

Secure your databases using DBA Security Advisor!

Generate dynamic T-SQL scripts with Dynamic SQL Generator!

Check our latest software releases!

Check our eBooks!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub