arttuladhar
6/7/2017 - 4:09 PM

Python Fundamentals

Python Fundamentals

# ********************************************************************
# Strings
# ********************************************************************

msg = 'Hello World'
anotherMsg = "Hello User"
print msg
print anotherMsg

firstName = 'John'
lastName = 'Goldo'
fullName = firstName + ' ' + lastName
print fullName
print "In Uppercase -", fullName.upper()
print "In Lowercase -", fullName.lower()
print "Counting occurances of o -", fullName.count('o')
print "Using Replace -", fullName.replace('John', 'Bill')
print "Welcome - {0} from {1}".format(fullName, "ART")
print "Welcome - %s from %s" %(fullName, 'ART')
# ********************************************************************
# Lists
# ********************************************************************

users = ['John', 'Adam', 'Bob']
print users

# Size of List
print len(users)

# Getting First Item in List
print users[0]

# Getting Last Item in List
print users[-1]

# Looping Through List
for user in users:
    print 'Hello {0} !'.format(user)

# Adding Item to List
users.append('Nabin')
print users

# Making Numerical List
squares = [x**2 for x in range(1, 11)]
print squares

# Copying a List
usersCopy = users[:]
print usersCopy

# Checking
print 'Nabin' in usersCopy
# ********************************************************************
# Dictionaries
# ********************************************************************

# Initiating a Dictionary
nepal = {'name': 'Nepal', 'capital': 'Kathmandu'}

# Store Item in Dictionary
nepal['area'] = 147180

print nepal

# List All Keys
print nepal.keys()

# List All Values
print nepal.values()

# has_keys(k)
print nepal.has_key('capital')
# ********************************************************************
# Control Statements
# ********************************************************************

num = 20

### IF Statement
if num == 20:
    print 'The Number is 20'
else:
    print 'The Number is not 20'

### Else If Statement
num = 21
if num == 20:
    print 'The Number is 20'
elif num > 20:
    print 'Number is Greater Than 20'
elif num < 20:
    print 'Number if Less Than 20'
else:
    print 'Not a Valid Number'