PYTHON
1) To print something :-
Ex: Hello world
Print (“Hello World”)
Always use quotation marks (““) inside the brackets, or else it won’t print.
2) Drawing a shape:-
Ex: TRIANGLE
Code: print (“/ \”)
Python looks at the string “/ \”, the last character before the closing
is a backslash \.
In python, \ is not a normal character. It tells python:-
“The next character is special”.
But in your case, the \ was the last character- nothing came after it.
So python didn’t know what to do and it raised to and error:-
EO2 (End Of Line) while scanning string literal.
How to fix it?
In Python strings, the backslash \ is a special escape character. For example:
\n → new line
\t → tab
\\ → prints a single backslash
So when you write something like:
But if you just write "\", Python thinks you’re starting an
escape sequence, which causes errors or weird behavior.
That’s why you must use \\ whenever you want to print a
single backslash.
3) Variables and Data types: - stores certain data values which makes it easier
to work with and manage all the data types inside our program.
What if we want the change the name of the characters? We have to go
inside the story, look at every single line where the name of the character
is, and change it manually...and that consumes a lot of our time. AND WE
DON’T WANT THAT.
So, what we can do is use variables to store our character’s name and age, which
makes it a lot easier to put it inside our program.
The 3rd line means “data I need to print” + data stored in variables +
“needs to be printed”
We use “__ to show there is a space.
Make sure to keep some space before the quotes.
What if you want to change the name of the character half way of the
story?
WORKING WITH STRINGS:-
Strings are plane texts. Any text that we want to have inside our program, we can
store inside a string. To create a string we need to use quotation marks.
Ex: print (Giraffe Academy), if we want to create a new line inside of the string,
what we have to do is (Giraffe\nAcademy). So it will actually insert a new line into
the string.
It basically means draft on one line and Academy on the another line.
In addition to \n we can also use backslash quotation mark \”. So it basically
means hey I want to print a quotation [Link] is a escape character.
Backslash is a special character, and it tells Python that whatever
character comes after it, we want to render it.
String Variable
Concatenation
The process of taking a string and appending another string on to it.
Functions
It is a block of code that we can run. And it will perform a specific operation
for us. We can use strings to modify our strings and to get information
about our strings.
We can use functions to convert our strings to lower or upper case.
How to check if your string is entirely uppercase/ lowercase?
So if I write [Link] () so if it is entirely uppercase it would be TRUE and if
not its FALSE.
So [Link] is gonna convert it into uppercase and its gonna say that it’s
TRUE.
To find the length of the string/ how many characters are there in the string?
String have an index starting with 0.
INDEX FUNCTION
It tells us where a specific character/string is located inside our string.
Replace:-
WORKING WITH NUMBERS
In python, quotation marks are used to define string literals, which are sequences
of characters representing text. This distinction is crucial because python needs to
differentiate between literal texts and other elements of the language, such as
numbers, variables or keywords.
Like this you can also do subtraction, Division and multiplication.
Remainder:
So its gonna divide 10 by 3 , give the remainder.
Store numbers inside a variable:-
Converting number into a string:-
(it comes handy when u want to print numbers alongside string.
Use this “str” when u want to print a number next to a string.
Functions can perform mathematical operations. It could also give us
information about our number.
1. Abs (absolute number):-An "absolute number" is a synonym for a real
number, referring to any number without a sign (positive or negative)
that can be found on a number line
2) Pow: - it passes 2 pieces of information. Basically, first is a number
and the 2nd is going to be a power. (3,4) means 3^4
4) max & min :- prints out the max and min number.
5) round function:- rounds off values
Normally, if the decimal part is less than 0.5 → round down.
If the decimal part is more than 0.5 → round up.
But if it is exactly 0.5 → Python rounds to the nearest even number (this is called
banker’s rounding).
From math import*:- when we put this line, it gives us access to a lot of math
functions. It allows us to do a lot of math in our program.
1) floor:- removes the decimal point.
2) Look at the number.
3) If it already is an integer → stays the same (floor(6) = 6).
4) If it has decimals → go to the next whole number downward.
a. For positives → moves lower (6.9 → 6).
b. For negatives → still goes downward, away from zero (-6.1 → -7).
2)ceil:-
It always goes up to the next integer, no matter what the decimal part is.
"Up" means toward positive infinity (+∞).
3) Square root:-
GETTING INPUT FROM USERS
We are basically going to allow a user to input information in our program. Then
we are going to take that information that the user input, and store it inside a
variable. Then we can use it.
BUILDING A CALCULATOR:-
We will take 2 users from the user, add them and print them. First create 2
variables, then store the numbers u want to add in the variables.
Functions:
1)int it converts whatever there is inside the open and closed parentheses into
an integer. And integer is a whole number. So basically converts a string into an
integer. If decimals used, its gonna break our program.
2) float:- A number that has decimals.