PYTHON : ZTM - Section : 3 : 17. Learning Python

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 

 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

17. Learning Python

  •  Best Practices
  •  Terms
  •  Action 
  • Datatypes

 

19. Python Data Types

These are called the fundamental data types in python

  • int
  • float
  • bool
  • str
  • list
  • tuple
  • set 
  • dict

 Classes  -- beyond the datatype we can create something called classes. Custom type .

Specialized Data types -- They are not build into python . But they are special packages and     modules that we can use from libraries .

Whenever we do not have the data types in the standard data types and we do not want to create our own we can use the specialized datatype. We can from what we call modules. You can think of it as extensions and there is another type which is a little special called none.

None : The idea of 0 , it is the absents of value,

 We will start with the fundamental data type we will start with these two for now "int" and "float"

 
print(5 + 5)
print(6 -3)
print(6 / 3)
print(6 * 3)

print(type(5 + 5))   / this tell what type of data type it is returns - <class 'init' >

2 to the power of 2

( 2 ** 2)

(3 // 4 )  -- this actually return an int if and it is rounded to  -- This get rounded down to a int

(5 % 4) moduler operator tells you what is the reminder of the division


21. Math Functions

print(round(3.1))  -- rounding down the number

print(abs())   -- an abs returns the absolute value of the argument / An absolute value means no negative numbers

print(abs(-20)) -- absolute value of negative -20 is 20 (no negative values)

abs = absolute


22. DEVELOPER FUNDAMENTALS: I

How to avoid the mistakes that I made while I got started -- Tutor

https://docs.python.org/3/library/index.html


23. Operator Precedence

Difference operator have precedence over different ones

print(20 + 4 * 3) for instance 4*3 gets multipled first and the rest is added to 20


24. Exercise: Operator Precedence

Order of precedence

()
**
* & /
+ & -

There is an extra data type "complex" .

You real would be using this of you were doing complex math -- the teacher of this video never used it.

But it is good to know it exists and you can read about complex umber if you want .

------

The int value that is stored int and float - the binary values

bin(5)  -- This is going to return whatever is the binary version of the file is

>>> print(bin(50))

result : '0b110010'

if I want to convert this into a integer

We tell python we want to convert that into an integer which has the base 10

>>> int( '0b110010' , 2 )

>>> print(int('0b110010', 2))


Variable store information in order to use it in the programs . Assigning a value to a variable is also called as binding .

Rules of writing variable.


  1. Snake case -- means they are all lower case and space does not exist they are all underscore. 
  2. Variables must start with a lower case or an underscore. 
  3. Variables can be anything with letters, numbers or underscores. But remember they have to start with lower case and underscore.
  4. lets say we have created a variable "snake_casE" with a capital E , that will be different variable. 
  5. You cannot overwrite keywords.

 First the variable has to be in the form of a snake_case

_variable_name -- under scope in python signifies a private variable.  example 

print is a keyword in python , so you cannot create a variable name with a keyword that python program uses for specific purposes.

Google  - Python Keywords

https://www.w3schools.com/python/python_ref_keywords.asp

to tell if it is a key word or an important word in python it is highlighted in Blue.

iq = 190
user_iq = iq/4
print(user_iq)

#constants 

PI = 3.15

When the variable is in capital letters it means to say that they are constant variables . Another type of

Two underscores - and we call this redundant


You should not create a variable with two underscore .

Another way to assign values to variables multiple times.

a,b.c = 1,2,3 

The below works

a,b,c = 1,2,3
print(a)
print(b,c)

---below works as well

a,b,c = 1,2,3
print(a,b,c)
print(b,c)


27. Expressions vs Statements

Expression 

iq = 100

user_age = iq / 5    -- An expression is this part of the code. It is a piece of code that produces a value.

Statements

  • iq = 100
  • user_age = iq / 5  


28. Augmented Assignment Operator

#Augmented Assignment Operator

some_value = 5

some_value = 5 + 2  -- you can also write it as  > some_value = some_value + 2

You can do the same as above using an augmented Assignment Operator

> some_value += 2

  • +=
  • -=
  • *=
  • /=

29. Strings

 A string is simply a piece of text . A string can be written in quotation marks  > 'hi'

  • 'hi there , how are you'
  • "hi there , how are you"
  •  "hi there , how are you 24!"   -- we can put numbers into it . It becomes a piece of text

Single quote and Double Quote are valid strings

 And if you use type = print(type("Hello , You there"))

Imaging you are collecting the login form

username = 'supercoder'

password = 'supercoder'

There is a third way to write strings

long_string = '''

WOW

I am here

'''

Using three single quote ''' you can gather multiple lines '''

  • first_name = 'Sreejith'
    last_name  = 'Balakrishnan'
    fullname   = first_name + '  ' + last_name
    print(fullname)

 

30. String Concatenation

# String Concatenation

It simply means adding strings together .

print("Hello " + "Sreejith")

String concatenation only works with strings

print("Hello" + 5)   -- does not work


31. Type Conversion

Lets see 

str(100)  -- 100 is clearly a number but what if we run  a string function on this

print(str(100))   -- If we run this 

is this string or int lets use the below.

print(type(str(100)))   - so this has converted 100 into string

<class 'str'>

print(type(int(str(100))))

Above example converts 100 into a string and back into a integer

The same can be achieved 

a = str(100)
b = int(a)
c = type(b)
print(c)


32. Escape Sequences

#escape sequences

weather = ' weather's  sunny'   --

code :

weather = "Weather\'s \"kind of\" sunny"
print(weather)

--

weather = "Weather\t\'s \"kind of\" sunny"
print(weather)

\t - tab

\n - newline

--

weather = "Weather\t\'s \"kind of\" sunny \n hope you have a good day"
print(weather)


33. Formatted Strings

#Formatted Strings

When a user logs into their profile we want to display their name for what they have in the Kart

We should have something dynamic to store names of the customers 

code :

name = 'Sreejith'
print('Hi ' +name)

name = 'Sreejith'
age = 40
print('Hi '+ name + '.  your age is ' + str(age) )

or

name = 'Sreejith'
age = 40
print('Hi '+ name + '.  Your age is ' + str(age) )

instead of the above code -- we can simply say that this is formatted string.

name = 'Sreejith'
age = 40
print(f'Hi {name} . Your age is {age} ')

This is a new feature of python 3  . By adding f to the beginning you are saying it is going to be a formatted string. 

The other way of writting this code is

name = 'Sreejith'
age = 40
print('Hi {} . Your age is {} now'.format('Sreejith','40'))

whatever comes first in the bracket is taken as first value in the brackets above

Now what if we want to use variables.

name = 'Sreejith'
age = 40
print('Hi {} . Your age is {} now'.format(name,age))

what if we wanted it in specific order , name in the place of age and age in the place of name. Something like giving index number starting from 0 is what is counted in python .

name = 'Sreejith'
age = 40
print('Hi {1} . Your age is {0} now'.format(name,age))

You can create your own variable .

name = 'Sreejith'
age = 40
print('Hi {new_name} . Your age is {age} now'.format(new_name='Daigeo',age=41))

As you can see with  .format   things are little complicated, you will still see this in all Python 2 , code still uses the .format .  And you will see a loads of people still using it in Python 3

I would this format is the easiest .

name = 'Sreejith'
age = 40
print(f'Hi {name} . Your age is {age} now')


34. String Indexes

Up to this point we learned that "str" is an ordered sequence of characters . It is essentially a piece of text what we can write our names , password, paragraphs and sentences .

And str underneath the hood are stored in memory .

The memory address that is stored in the memory by the computer 



>>> print(selfish)

Python has this unique feature that helps us grab different pieces of text .

When we use [] square brackets in python and .. The first item that we put between the square brackets is what we call the start . The start is where do you want me to look . But there is an extra thing that we can do . 

[start:stop:stepover]

We also have the option to say where to stop.

selfish = '01234567'
print(selfish[0:2])

01 is display, stop it here before it get to shelf 2.

[start:stop:stepover]

Stepover says says start here [0:8] , start at 0 and end at 8 and stepover a few things .

The default is 1 as we are going one by one for -stepover

selfish = '01234567'
print(selfish[0:8:2])

Result : 0246

--

selfish = '01234567'
print(selfish[1:])

The print statement says start with 1 an after : there is no value so it goes all the way to the end

Result : 1234567

--

selfish = '01234567'
print(selfish[:5])

Result : 01234

--

selfish = '01234567'
print(selfish[::1])

Result : 01234567

--

selfish = '01234567'
print(selfish[-1])

Result : 7 

-1 starts at the end of the string in python

--

selfish = '01234567'
print(selfish[::-1])

Result : 76543210

--

 If I say skip by 2 

selfish = '01234567'
print(selfish[::-2])

Result : 7531

--

Up until now we learned that strings can be accessed quite easily . and using the [] we access different parts of the string.

And this idea of a start | stop | stepover  -- is what we call slicing

selfish([start:stop:stepover])

Strings in python are immutable - what do I mean by that ?

code

selfish = '01234567'
selfish[0] = 8
print(selfish)

You cannot change the value of the string . you have completely reassign the value for in memory .

for example

selfish = 8

The one way to change the string is to completely re-assign the value .

36. Built-In Functions + Methods

So far we have learned a few build-in functions that Python has










 


Comments

Popular posts from this blog

Datatypes

PYTHON : In 4 hours

Complete Python Developer in 2021 : Zero to Mastery :::