Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
# CONDITIONALS -------------
# The if, else and elif statements are used to perform different
# actions based off of conditions
# Comparison Operators : ==, !=, >, <, >=, <=
# The if statement will execute code if a condition is met
# White space is used to group blocks of code in Python
# Use the same number of proceeding spaces for blocks of code
age = 30
if age > 16 :
print('You are old enough to drive')
# Use an if statement if you want to execute different code regardless
# of whether the condition ws met or not
if age > 16 :
print('You are old enough to drive')
else :
print('You are not old enough to drive')
# If you want to check for multiple conditions use elif
# If the first matches it won't check other conditions that follow
if age >= 21 :
print('You are old enough to drive a tractor trailer')
elif age >= 16:
print('You are old enough to drive a car')
else :
print('You are not old enough to drive')
# You can combine conditions with logical operators
# Logical Operators : and, or, not
if ((age >= 1) and (age <= 18)):
print("You get a birthday party")
elif (age == 21) or (age >= 65):
print("You get a birthday party")
elif not(age == 30):
print("You don't get a birthday party")
else:
print("You get a birthday party yeah")
# FOR LOOPS -------------
# Allows you to perform an action a set number of times
# Range performs the action 10 times 0 - 9
for x in range(0, 10):
print(x , ' ', end="")
print('\n')
# You can use for loops to cycle through a list
grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas']
for y in grocery_list:
print(y)
# You can also define a list of numbers to cycle through
for x in [2,4,6,8,10]:
print(x)
# You can double up for loops to cycle through lists
num_list =[[1,2,3],[10,20,30],[100,200,300]];
for x in range(0,3):
for y in range(0,3):
print(num_list[x][y])
# WHILE LOOPS -------------
# While loops are used when you don't know ahead of time how many
# times you'll have to loop
random_num = random.randrange(0,100)
while (random_num != 15):
print(random_num)
random_num = random.randrange(0,100)
# An iterator for a while loop is defined before the loop
i = 0;
while (i <= 20):
if(i%2 == 0):
print(i)
elif(i == 9):
# Forces the loop to end all together
break
else:
# Shorthand for i = i + 1
i += 1
# Skips to the next iteration of the loop
continue
i += 1
# FUNCTIONS -------------
# Functions allow you to reuse and write readable code
# Type def (define), function name and parameters it receives
# return is used to return something to the caller of the function
def addNumbers(fNum, sNum):
sumNum = fNum + sNum
return sumNum
print(addNumbers(1, 4))
# Can't get the value of rNum because it was created in a function
# It is said to be out of scope
# print(sumNum)
# If you define a variable outside of the function it works every place
newNum = 0;
def subNumbers(fNum, sNum):
newNum = fNum - sNum
return newNum
print(subNumbers(1, 4))
# USER INPUT -------------
print('What is your name?')
# Stores everything typed up until ENTER
name = sys.stdin.readline()
print('Hello', name)
# STRINGS -------------
# A string is a series of characters surrounded by ' or "
long_string = "I'll catch you if you fall - The Floor"
# Retrieve the first 4 characters
print(long_string[0:4])
# Get the last 5 characters
print(long_string[-5:])
# Everything up to the last 5 characters
print(long_string[:-5])
# Concatenate part of a string to another
print(long_string[:4] + " be there")
# String formatting
print("%c is my %s letter and my number %d number is %.5f" % ('X', 'favorite', 1, .14))
# Capitalizes the first letter
print(long_string.capitalize())
# Returns the index of the start of the string
# case sensitive
print(long_string.find("Floor"))
# Returns true if all characters are letters ' isn't a letter
print(long_string.isalpha())
# Returns true if all characters are numbers
print(long_string.isalnum())
# Returns the string length
print(len(long_string))
# Replace the first word with the second (Add a number to replace more)
print(long_string.replace("Floor", "Ground"))
# Remove white space from front and end
print(long_string.strip())
# Split a string into a list based on the delimiter you provide
quote_list = long_string.split(" ")
print(quote_list)
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more