Learning Python has taken centerstage for many developers as Python is one of the key language for working in the field of data science/machine learning. If you are an experienced developer, this post would help you quickly get started with Python programming.
In this post, you will quickly learn some of the following in relation to Python programming:
subjects = ['Maths', 'English', 'Hindi']
fileVar = open("/home/support/Documents/hello.txt", "r"); print(fileVar.read())
When requiring to read lines, following methods can be used:
fileVar.readline() // Read one line fileVar.readlines() // Read all lines for line in fileVar: // Read line-by-line print(line)
fileVar = open("/home/support/Documents/hello.txt", "w") // Write mode; File is rewritten fileVar = open("/home/support/Documents/hello.txt", "a") // Append mode fileVar = open("/home/support/Documents/hello.txt", "r+") // Read-append mode fileVar.write("Hey, Whassup\n")
def functionName(parameters): function code goes here
The following is a sample code representing a function, calculateInterest, and the code invoking the function:
def calculateInterest(amount, roi, yrs): total = (amount * pow(1 + (roi/100), yrs)) interest = total - amount return interest print('Interest Calculator:') # # Take the user inputs # amount = float(input('Principal amount ?')) roi = float(input('Rate of Interest ?')) yrs = int(input('Duration (no. of years) ?')) # # Invoke the function to calculate the interest # interest = calculateInterest(amount, roi, yrs) # # Print the interest # print('\nInterest calculated = %0.2f' %interest)
if condition_expression: code_goes_here else: code_goes_here
The following represents a sample code:
if(interest > 100): print('Interest is greater than 100') else: print('Interest is less than 100')
for variable in range(lowerlimit, upperlimit): code_goes_here
The following is a sample code:
count = 0 lowerlimit = 0 upperlimit = 3 for count in range(lowerlimit, upperlimit): print 'hello world'
while conditional_expression: code_goes_here
The following is the sample code:
count = 0 upperlimit = 2 while count < upperlimit: print 'hello world' count += 1;
strVar = 'Hello world, how are you doing?' strVar.lower() // Change the string into lowercase strVar.upper() // Change the string into uppercase strVar.find('how') // Prints 13; Sstring index starts with 0 strVar.replace('world', 'vitalflux') // Replace the occurence of world with vitalflux strVar.split(' ') // Split the string with delimiter
def add(number1, number2): return number1 + number2 def subtract(number1, number2): return number1 - number2 def multiply(number1, number2): return number1*number2 def divide(number1, number2): return number1/number2
The module can be imported in another file such as hello.py.
import maths print(maths.add(10, 20)) print(maths.subtract(10, 20)) print(maths.multiply(10, 20)) print(maths.divide(10, 20))
def printHello(name): print('Hello ' + name) class MachineLearner: def __init__(self, name): self.name = name def toString(self): return self.name def sayHello(self): print('hello ' + self.name)
from ml import MachineLearner, printHello # Instantiate the class mlearner = MachineLearner('HelloWorld') print(mlearner.toString()) mlearner.sayHello() # Use printHello method printHello('Vitalflux')
try: f = open('/home/support/Documents/hello.txt') s = f.readline() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror)
In this post, you learned about how to get started Python programming if you are an experienced developer.
Did you find this article useful? Do you have any questions or suggestions about this article in relation to getting started with Python programming? Leave a comment and ask your questions and I shall do my best to address your queries.
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…