Python

By default, each print statement prints text in a new line. If we want multiple print statements to print in the same line, we can use the following code:
print("Sasmit",end =" ")
print("is working in capgemini")
o/p:
Sasmit is working in capgemini
Comments #
Comments are pieces of text used to describe what is happening in the code. They have no effect on the code whatsoever.
A comment can be written using the # character:
if u want to write multi-line comments:
then enclosed with """(triple quotes)
print(""" Hi This is Sasmit from
jungle and you can have a cup of coffee with me at evening time""")
o/p:
 Hi This is Sasmit from
jungle and you can have a cup of coffee with me at evening time
DataTypes:
The language provides three main data types:
Numbers
Strings
Booleans
Variables are mutable. Hence, the value of a variable can always be updated or replaced.
To create complex number:
complex(real, imaginary)
eg: print(complex(10, 20))
o/p: 10+20j
complex_3 = complex(0,0)
print(complex_3)
o/p: 0j
A complex number usually takes up 32 bytes of memory.
A float occupies 24 bytes of memory.
0 will take up 24 bytes whereas 1 would occupy 28 bytes.
print('Hero')
print("Hero")
o/p:
Hero
Hero
String Slicing:
string[start:end]
start is the index from where we want the substring to start.
end is the index where we want our substring to end.
The character at the end index in the string, will not be included in the substring obtained through this method.
another_string = "Kaise Kahun"
print(another_string[0:5])
o/p -
Kaise
Slicing with a Step #
string[start:end:step]
my_string = "Sasmit"
print(my_string[0:6])  # A step of 1
print(my_string[0:6:2])  # A step of 2
print(my_string[0:6:5])  # A step of 5
o/p: Sasmit
Ssi(skip 1 character)
St(skip 4 characters)
Reverse Slicing #
my_string = "This is MY string!"
print(my_string[13:2:-1]) # Take 1 step back each time
print(my_string[17:0:-2]) # Take 2 steps back. The opposite of what happens in the slide above
Partial Slicing #
If start is not provided, the substring will have all the characters until the end index.
If end is not provided, the substring will begin from the start index and go all the way to the end.
another_string ="Sasmit"
print(another_string[4:])
starts from 4 and ends at end
o/p: it
In general, Python’s operators follow the in-fix or prefix notations.
In-fix operators appear between two operands (values on which the operator acts) and hence, are usually known as binary operators:
A prefix operator usually works on one operand and appears before it. Hence, prefix operators are known as unary operators:
The 5 main operator types in Python are:
arithmetic operators
comparison operators
assignment operators
logical operators
bitwise operators
A division operation always results in a floating-point number.

Comments

Popular posts from this blog

scala-4