Skip to content

Instantly share code, notes, and snippets.

@Fogest
Created November 12, 2014 16:44
Show Gist options
  • Save Fogest/101226970b180d1cbc16 to your computer and use it in GitHub Desktop.
Save Fogest/101226970b180d1cbc16 to your computer and use it in GitHub Desktop.
Example of what you probably will need to know for CIS 1250 exam
#!/usr/bin/python
#You need to be able to declare variables and take in inputs, so let's do that!
userInputString = raw_input() #Input: Hello World (String)
userInputInt = raw_input() #Input: 10 (int)
userInputFloat = raw_input() #Input: 10.532523 (Float)
#We want userInputInt to be an integer, right now it is stored as a string. Convert to int:
userInputInt = int(userInputInt)
#We also need to convert userInputFloat to a float as it is also a string right now:
userInputFloat = float(userInputFloat)
#Say we want this int back to a string for some reason:
makeItAString = str(userInputInt) #Bam back to it.
#Float is the same:
alsoMakeItAString = str(userInputFloat)
#Okay, now let's do some math! We'll start with some new variables!
x = 2
y = 4
#Let's add them together:
z = x + y #Result: 6.
#Or even subtract:
z = y - x #Result: 2
#Multiply:
z = x * y #Result: 8
#And lastly we can divide:
z = y / x #Result: 2
#We could print all this stuff to the screen now too! Let's do some examples:
#Printing one thing:
print z
#Okay, we can do more than that, let's tell the user what the 'z' variable is:
print "The result of variable y divided by variable x is:",z
#Don't forget about concatination!!!
string1 = "Hello"
string2 = "World"
print string1 + string2 #Result: HelloWorld
#You can also put variables/numbers right in the middle of things using commas in print:
print "Hello",10,"World" #result: Hello 10 World
#Notice the spaces in the above output. Commas in print mean automatic spaces added. You could also do this with a vairable:
num = 10
print "Hello",num,"World" #Result: Hello 10 World
#Let's move on to some basic if statements. We want to print whether a number is bigger to another number.
numA = 10
numB = 20
#Which is bigger?
if numA > numB:
print numA, "is bigger than", numB
else:
print numB, "is bigger than", numA
#This is probably not needed for the exam, but you could also use logical operators.
numA = 20
numB = 10
numC = 1
if numA > numB AND numC == 1:
print numA, "is greater than", numB, "and C is equal to 1!"
#Or we could use a OR operator
elif: numA > numB OR numC == 1:
print "See, or works too!"
#Let's count from 1 to 5 using a for loop!
for i in range(1,6):
print i
"""
Result:
1
2
3
4
5
"""
#We can use a while loop as well
counter = 1
while counter < 6:
print counter
counter = counter + 1
"""
Result:
1
2
3
4
5
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment