Indentations Variables Variable Type Variable Type Casting in Python

I am sharing my little experience on Python programs. These are basic and helpful for beginners. First install python from https://www.python.org/downloads/

After download I install at below location(optional)



Now I created a BasicProgram.py file to write python program on same location(optional). You can change file location as per your cod collections.




Print a string in Python

def main():

#print something on console

print ("This is Sharjeels Work!")

main()

To run this program go to location where BasicProgram.py file is placed and write command as in above program main is a method to start the program and # is used for single line comment only. Print method used to print on console.

Few more basic operation on string like upper case , lower case conversion, getting string length, split string, remove white space etc.

def main() : 
employeeName=" sharjeel-bilali "
#Get the character at position 3 (remember that the first character has the position 0)
print("character at position 3 is : " + employeeName[3])

#Substring. Get the characters from position 2 to position 5 (not included)
print("characters from position 2 to position 6 are : " + employeeName[2:6])

#The strip() method removes any whitespace from the beginning or the end
print("removes any whitespace from the beginning or the end : " + employeeName.strip()) 

#The len() method returns the length of a string
print("length of a string is : ", len(employeeName))

#The lower() method returns the string in lower case
print("string in lower case : " + employeeName.lower())

#The upper() method returns the string in upper case
print("string in upper case : " +  employeeName.upper())

#The replace() method replaces a string with another string
print("replaces a string with another string : " + employeeName.replace("e", "a"))

#The split() method splits the string into substrings if it finds instances of the separator
print(" splits the string into substrings if it finds instances of the separator : ",  employeeName.split("-"))

main()


Python Indentations
Where in other programming languages the indentation in code is for readability only, in Python the indentation is very important.Python uses indentation to indicate a block of code.

def main():
#Correct way to use Identations
if 5 > 2:
print("It's Sharjeels Work!")

#Wrong way to use Identations
if 5 > 2:
print("It's not a Sharjeels Work!")
main()

Variables
Python does not requires any command to declare a variable.A variable is created the moment you first assign a value to it.

def main():
#Variable for Age, Name and Salary
employeeName="Sharjeel Bilali"
emplayeeAge = 25
employeeSalary = "125000"
print("Employee Salary " + employeeName)
print(emplayeeAge)
print("Employee Salary " + employeeSalary)
main()




Variable Type | Python Numbers

There are three numeric types in Python: int, float, complex

Variables of numeric types are created when you assign a value to them:

def main() : 
#Variable for Age, Name and Salary
employeeAge = 21    # int
employeePackage = 2.8  # float
employeeId = 1j   # complex
#To verify the type of any object in Python, use the type() function:
print(type(employeeAge))
print(type(employeePackage))
print(type(employeeId))
main()




Variable Type Casting

There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.Casting in python is therefore done using constructor functions:

int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)

float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)

str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals

def main():
#integer 
employeeId = int(1004)   # employeeId will be 1004
rate = int(2.8) # rate will be 2
employeeAge = int("33") # employeeAge will be 33

#Floats
a = float(1)     # a will be 1.0
b = float(2.8)   # b will be 2.8
c = float("3")   # c will be 3.0
d = float("4.2") # d will be 4.2

#Strings 
e = str("sharjeel") # e will be 'sharjeel'
f = str(2)    # f will be '2'
g = str(3.0)  # g will be '3.0'
main()



Comments

Post a Comment

Popular posts from this blog

Error : DependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: com.adobe.aem:uber-jar:jar:apis -> version 6.3.0 vs 6.4.0

Operators in Asterisk with Linux

ERROR Exception while handling event Sitecore.Eventing.Remote.PublishEndRemoteEventException: System.AggregateExceptionMessage: One or more exceptions occurred while processing the subscribers to the 'publish:end:remote'