All the function learnt this week
Print :Print, does what it implies, it prints what ever you put with in those brackets.
Example - print("Hello, World!")
SyntaxError or NameError:
Syntax or Name error, pops up in red and highlights where or which line the computer cant read. These errors can be spelling, grammar or the wrong function
Example - print "Hello" -- Here the brackets are left out and wont print
Variables :
Variables are simply used for storing data and use the operator = . Using a word or character of your choice you can store all the data, to use in other parts of your code
Example - msg = "Hi there" This can make code neat and easier when you have multiple variables
print(msg)
Grammar - Quotation & Commas:
Commas are used to separate different variables or phrases
Example - print(firstname, lastname)
Quotation marks are used a lot in python. Use can use either singe or double quotation marks, but just make sure to close with the same type. Also if you use double on the outer if you want quotation for grammar use single inside. quotations can also be used for spaces when using print.
Example - print("Harry" + " " + "Potter") ---or----- 'One does not simply "' + verb + '" into Mordor!'
Input:
Input is mainly used for asking a question or to get data from the user. It is always used with brackets and a variable so it can be used in the print function.
Example - name = input("What is your name? ")
print(name)
Joining messages:
To join different variables or phrases you can use +
Example- ( word +" "+ word +" "+ word)
Maths in Python:
addition | |
- | subtraction |
* | multiplication |
/ | division |
** | to the power of |
Example- n = 10
print(n**2)
Example- print("ab"*5)
Length:
The length function counts the length of the string. len is used with brackets
Example- print(len('Hello World!'))---- or------ a = "abcdefghijklmnopqrstuvwxyz"
print(len(a))
Converting Strings to Numbers (int)
When using string as a number you have to convert it into integer using int, use this function with brackets.
Example- number = int(input('Enter a number: '))
new_number = number - 1
print(new_number)
Converting Numbers to Strings (str)
This is simply converting the integer into a string
Example- answer = 5
print("the answer is " + str(answer))
print("the answer is " + str(answer))
No comments:
Post a Comment