Last active
June 7, 2017 16:11
-
-
Save arttuladhar/e2fb094fc091da88298a983096d07f6d to your computer and use it in GitHub Desktop.
Python Fundamentals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ******************************************************************** | |
# 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ******************************************************************** | |
# 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ******************************************************************** | |
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ******************************************************************** | |
# 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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment