0% found this document useful (0 votes)
8 views5 pages

Day - 2

Uploaded by

akshayvarma13187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Day - 2

Uploaded by

akshayvarma13187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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)

Assigning Multiple Values:


1) Many values to multiple variables
Python allows you to assign values to multiple variables in a single line
X, x, Age = 10, 11, 12
2) One value to multiple variables
X=x=Age=10
3) Output variables
Python print() function is often used to output variables

In the print() function, you output multiple variables, separated by a comma


X, x, age = “python”, “is”, 30
print(X, x, age)

You can also use the + operator to output multiple variables


X, x, Age = ‘Python”, “Is”, “Awesome”
print(X+x+Age)

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)

What if I want to convert a certain local variable to global?


We can use the global keyword to create a global variable inside your own function
—----------------------------------------------------------------------------------------
---------
def myfunc():
Global x
x = 10

print(x)

myfunc()

print(x)
—----------------------------------------------------------------------------------------
---------

Built in data types:


1) Text Type - str (strings) ..
2) Numeric Type - int (integer), float(decimals), complex ..
3) Sequenced type - list, tuple, *range
4) Mapping type - dict(dictionaries)
5) Set type - set, *frozenset
6) Boolean type - bool(true or false)
7) *binary type - bytes, bytearray, memoryview
8) *nonetype - NoneType

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

-> int to float


-> int to complex are possible
-> float to int
-> float to complex

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

You might also like