Day - 2
Day - 2
Indentation:
Indentation refers to the space at the beginning of a code line
In other programming languages the indentation is only for code readability only, but in
python its very very important
Python uses indentation to indicate a block of code
If we dont use indentation it gives you an error
Variables:-
Creating Variables:
1) Python has no command for declaring variable
2) A variable is created the moment you assign a value to it
Variable names:
1) A variable name must start with a letter or the underscore character
2) A variable name cannot start with a number
3) A variable name can only contain alpha-numeric characters and underscores(
A-z, 0-9 and _)
4) Variable names are case sensitive( AGE, age, Age, AGe, aGe all 5 different
variables)
5) A variable name cannot be any of the python keywords.
Multi-Words variable:
1) Camel Case - Each word, excerpt the first starts with a capital
(myVariableName)
2) Pascal Case - Each word starts capital letter (MyVariableName)
3) Snake case - Each word is separated by an underscore character
(my_variable_name)
Types of variables:
1) Local Variable - Variables that are created inside a function and only can
be called for tasks inside the function
2) Global Variable - Variables that are created outside of a function are
known as global variables ( as in all the examples in the previous cases)
print(x)
myfunc()
print(x)
—----------------------------------------------------------------------------------------
---------
Python Casting:-
Specify a variable type:
When you want to specify a type on to a variable, this is done by casting.
Casting in python is done using constructor functions:
1) int() - constructs an integer number from an integer literal, a float literal(by
removing all decimals), or a string literal(providing the string represents a whole
number).
2) float() - constructs a float number from an integer literal, a float literal or a
string literal (providing the string represents either an integer or a float)
3) str() - constructs a string from a wide variety of data types, including integers,
float literals or integer literals
But, complex to int or complex to float is absolutely not possible (you cannot convert
complex to any number type)
x = 1 #x will be 1
y = int(2.8) # y will be 2
z = int(“#”) # z will be 3
Integer:
Int or integer is a whole number either positive or negative, without decimals, of
unlimited length
Ex - 1, 3565622488771, -738573573848734
Float:
Float, or “floating point number” is a number, positive or negative, containing one or
more decimals
Ex - 1.10, 1.0, -35.69
Floats can also be scientific numbers with a “e” (euler’s constant) to indicate the
power of 10.
Ex - 35e3, 12E4, -87.7e100
( “e”, or “E” its the same when mentioning scientific numbers in float and python)
Complex:
Complex numbers are written with a “j” as the imaginary part
Ex - 3+5j, 5j, -5j