Upload
nexthoughts-technologies
View
200
Download
0
Embed Size (px)
Citation preview
PYTHON BASICS
Basics
1) To output simple use print : print(‘Hello World’)
2)When you put the string in triple quotes, it will be displayed the way you have the string in the text editorprint(“””The output would be in ! MULTIPLE LINES”””)or you could just use \n
3) In Python we use a # to indicate comments : #This is a comment print('Hello World’)
4)
Taking Input
firstName = input("What is your first name? ")lastName = input("What is your last name? " )print("Hello" + firstName + lastName)
Strings and methods to manipulate themmessage = 'Hello world' print(message.lower()) print(message.upper()) print(message.swapcase())print(message.find('world')) print(message.count('o'))print(message.capitalize()) print(message.replace('Hello','Hi'))
Type Conversion
. Ex:-salary = input("Please enter your salary: ")bonus = input("Please enter your bonus: ")payCheck = salary + bonusprint(payCheck). How to:-int(value) converts to an integerlong(value) converts to a long integeretc.
Working with dates
• The datetime class allows us to get the current date and time• Ex:- import datetime currentDate = datetime.date.today() print (currentDate)print (currentDate.year) print (currentDate.month) print (currentDate.day)print (currentDate.strftime('%d %b,%Y’))print (currentDate.strftime('Please attend our event %A, %B %d in the year %Y'))• %d is the day of the month• %b is the abbreviation for the month• %Y is the 4 digit year • For more you can visit strftime.org• To add or subtract days or hours or minutes, use timedelta methodprint (currentDate + datetime.timedelta(days=-15))
What about times?
• It is called Datetime, so yes, it can store times.import datetime currentTime = datetime.datetime.now() print (currentTime) print (currentTime.hour) print (currentTime.minute)print (currentTime.second)print (datetime.datetime.strftime(currentTime,’%H:%M'))
%H Hours (24 hr clock)%I Hours (12 hr clock)%p AM or PM%m Minutes%S Seconds
Making decisions
-if os is not None : print(“Amazing choice”)elif not os == "windows" : print(“Amazing choice again ! ”)else: print(“Joke Joke ! ”)
-if saturday or sunday : print(“Beer")-if wednesday and yourSession : print("DAMN")
LOOOOOOPS
-for steps in range(1,4) :print(steps)
output- 1 2 3-for steps in range(4) :
print(steps)output- 0 1 2 3
-for steps in range(1,10,2) :print(steps)
output- 1 3 5 7 9-for steps in [1,2,3,4,5] :
print(steps)
Phun with loops and turtle
import turtle for steps in ['red','blue','green','black'] : turtle.color(steps) turtle.forward(100) turtle.right(90)
List
• Ex [0], [2.3,4.5],[5,”hello”,9.8]
• Use len to get the length of list ex:-
Example:- names=[“amit”,”divyansh”,”kshitij”]
len(names)
output=3
List Methodslaptops = [‘Blade','Alienware','Macbook','MSI']#add a new value to the end of the list laptops.append('Microsoft')
#display the last value in the list print(laptops[-1])
#remove a value from the lustlaptops.remove('Alienware')
#delete the first item in the list del laptops[0]
#print the first item in the list print(laptops[0])
#this will return the index in the list print(laptops.index(‘Macbook'))
#Sort the names in alphabetical order laptops.sort()#Otherslaptops.upper(), laptops.lower()
Exception Handling
first = input("Enter the first number ") second = input("Enter the second number ") firstNumber = float(first) secondNumber = float(second) try : result = firstNumber / secondNumber print (first + " / " + second + " = " + str(result))except Exception as e: print(e)– https://
docs.python.org/3/c-api/exceptions.html#standard-exceptions