Python Programming Tutorial: Loops, Strings, and Lists
Posted on Apr 26, 2024 in Computers
Loops
Printing Natural Numbers
n=1
while n<=50:
print(n)
n=n+1
Printing Years of Your Life
year=int(input("Enter the year you were born: "))
while year <=2019:
print(year)
year=year+1
Final Countdown
n=10
while n>0:
print(n)
n=n-1
print("lift off!")
Printing Multiples of 3
n=3
while n<100:
print(n)
n=n+3
Candy Distribution
candies= int(input("Enter the number of candies to distribute: "))
n=1 #number of friends
while candies>0:
candies=candies-n**2
n=n+1
print("I have distributed between this number of friends", n-1)
FizzBuzz Problem
n=1
while n<=100:
if n%3==0 and n%5!=0:
print("fizz")
elif n%3!=0 and n%5==0:
print("buzz")
elif n%3==0 and n%5==0:
print("fizzbuzz")
else:
print(n)
n=n+1
Finding Prime Numbers
factor=2
number=150
while factor
Strings
Sentence Manipulation
sentence=input("enter a sentence: ")
x=len(sentence)
print("the length of the sentence is ",x)
half=int(x/2) #because is the lengh is odd, we cannot use a decimal number inside the []
print(sentence[half:]+sentence[:half])
Counting Character Occurrences
magicword="abracadabra"
times=magicword.count("a") #to use it, it must be written as string, count("characters")
print("the number of times the character a appears in the word is", times)
Checking Substring Presence
check="to be"
quote="to be or not to be, that is the question"
if check in quote:
print("check is contained in quote")
else:
print("it isn't contained")
Finding Waldo
circus = "noah olivia brandon emma keshav omar liam ava sphia isabella mason mia michael carter madison william walden john abigail andrew joseph"
food = "oliver kanye christopher abhinav sebastian jackson david andrew wyatt wilson waldo william sofia leika aaron jessica emily alexeander"
rides = "sean beyonce lucas ethan benjamin alexander william joseph madison wyatt johnny harrison julian william charlotte logan alexander jospeh samir"
if "waldo" in circus:
print("waldo is at circus")
if "waldo" in food:
print("waldo is at food")
if "waldo" in rides:
print("waldo is at rides")
Finding Substring Index
if check in quote:
print(quote.index("to be"))
Replacing Substrings
print(quote.replace("e","z"))
String Manipulation Example
necklace="wbwwbwbbbwbww"
necklace=necklace.replace("w","x")
necklace=necklace.replace("b","bw")
necklace=necklace.replace("x","b")
print(necklace)
Accessing Characters
quote="to be or not to be? That is the question"
print(quote[0])
print(quote[21])
print(quote[-1])#to refer to the last character of the string
print(quote[-5])
print(quote[3:11]) #to obtain a substring, enters the 1st but not the last
print(quote[:11]) #it is the same as quote [0:11]
print(quote[3:]) #it means to the end
print(quote[:]) #all the string
Sentence Validation
sentence=input("enter a sentence: ")
if sentence[-1]==("." or "?" or "!"):
print("this sentence is true")
else:
print("this sentence is false")
String Rearrangement
word="dkabraabraca"
print(word[5:]+word[:5])
Correcting Characters
author= "shakespeer"
author=author[:8]+"are"
print(author)
place="sancristobaldelalahun"
place=place[:-3]+"guna"
print(place)
print(len(place))
b=13
a=len(place) #it is an integer number
print=(a+b)
Lists
List Operations
usserials=["Game of thrones","prison break","vikings"]
upserials=["la casa de papel" ,"vis a vis", "elite"]
print(usserials+upserials) #we can sum lists, summing their elements
if "vikings" in usserials:
print("Yes, it is")
numbers15=(1,2,3,4,5)
numbers610=(6,7,8,9,10)
numbers=numbers15+numbers610
print(numbers)
Accessing List Elements
print(numbers[1])
print(numbers[-1])
print(numbers[:-2])
Copying Lists
copylist=numbers[:]
print(copylist)
Modifying List Elements
upserials[2]="cuentame"
print(upserials)
Swapping List Elements
temp=usserials[0]
usserials[0]=usserials[1]
usserials[1]=temp
print(usserials)
Finding List Length
print(len(usserials))
List Example: Countries Visited
countries=["France","Deuschtland","Italy","Switzerland","Liechtestein"]
print(len(countries))
temporal=countries[3]
countries[3]=countries[2]
countries[2]=temporal
print(countries[-3:])
Additional Examples
Summing Numbers Until 0
n=int(input("enter a number to sum"))
suma=0
while n!=0:
suma=suma+n
print(suma)
n=int(input("enter other number"))
Multiplying Numbers Until 0
n=int(input("enter a number to multiply"))
product=1
while n!=0:
product=product*n
print(product)
n=int(input("enter other number"))
List Manipulation Example
one=int(input("enter a value"))
two=int(input("enter a value"))
three=int(input("enter a value"))
lista=[one,two,three]
print(lista)
if two%2==0:
print("even value of the second")
else:
print("not even")
contar=len(lista)
print(contar)