PyCoders

PyCoders Python Codes ; Python Jokes ; Python Tips and tricks ; Python Memes ;
Get everything valuable in Python
Love Python ! Live Python

Hey fellow programmers out there . So looks like you still use print() to track events instead of logging module in pyth...
14/12/2020

Hey fellow programmers out there . So looks like you still use print() to track events instead of logging module in python. I know it's easy to just put a print statement but believe me logging has much more features than that and it looks stunning on the console or a file.
Beleive me it's very useful and is quite easy to learn.

Look at this simple code below. It logs the data to a file.

import logging

logging.basicConfig(filename="hello.log")

# You can use any one of the below
logging.debug("A debug log") # to store debug logs.
logging.info("An info log") # for info logs
logging.warning("A warning") # A warning log.
logging.error("Oops an error)
logging.critical("OMG ! Everybody run . Retreat")

So to lets dive deep in it.
Don't worry you won't be drowned. 😊

Logging is an essential tool for development as it helps us in keeping track of events, monitoring the program behavior, and debugging; and without it…

Some of the weirdest programming languages. .
18/11/2020

Some of the weirdest programming languages.

.

17/11/2020

17/05/2020
     # #
14/05/2020

# #

The item at a certain index in a list can be reassigned.For example: ----------Program--------nums = [7, 7, 7, 7, 7]nums...
09/05/2020

The item at a certain index in a list can be reassigned.
For example:

----------Program--------
nums = [7, 7, 7, 7, 7]
nums[2] = 5
print(nums)

----------OUTPUT----------
[7, 7, 5, 7, 7]

Lists can be added and multiplied in the same way as strings.
For example:

----------Program--------
nums = [1, 2, 3]
print(nums + [4, 5, 6])
print(nums * 3)

----------OUTPUT----------
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3]

Lists and strings are similar in many ways - strings can be thought of as lists of characters that can't be changed.
To check if an item is in a list, the in operator can be used. It returns True if the item occurs one or more times in the list, and False if it doesn't.

----------Program--------
words = ["spam", "egg", "spam", "sausage"]
print("spam" in words)
print("egg" in words)
print("tomato" in words)

----------OUTPUT----------
True
True
False

The in operator is also used to determine whether or not a string is a substring of another string.

To check if an item is not in a list, you can use the not operator in one of the following ways:

----------Program--------
nums = [1, 2, 3]
print(not 4 in nums)
print(4 not in nums)
print(not 3 in nums)
print(3 not in nums)

----------OUTPUT----------
True
True
False
False

Another way of altering lists is using the append method. This adds an item to the end of an existing list. The dot before append is there because it is a method of the list class. Methods will be explained in a later lesson.

----------Program----------
nums = [1, 2, 3]
nums.append(4)
print(nums)

----------OUTPUT----------
[1, 2, 3, 4]

To get the number of items in a list, you can use the len function. Unlike append, len is a normal function, rather than a method. This means it is written before the list it is being called on, without a dot.

----------Program---------
nums = [1, 3, 5, 2, 4]
print(len(nums))

----------OUTPUT----------
5

The insert method is similar to append, except that it allows you to insert a new item at any position in the list, as opposed to just at the end.

----------Program---------
words = ["Python", "fun"]
index = 1
words.insert(index, "is")
print(words)

----------OUTPUT----------
['Python', 'is', 'fun']

The index method finds the first occurrence of a list item and returns its index.
If the item isn't in the list, it raises a ValueError.

----------Program---------
letters = ['p', 'q', 'r', 's', 'p', 'u']
print(letters.index('r'))
print(letters.index('p'))
print(letters.index('z'))

----------OUTPUT----------
2
0
ValueError: 'z' is not in list

There are a few more useful functions and methods for lists.
max(list): Returns the list item with the maximum value
min(list): Returns the list item with minimum value
list.count(obj): Returns a count of how many times an item occurs in a list
list.remove(obj): Removes an object from a list
list.reverse(): Reverses objects in a list

For list basics https://www.facebook.com/114683366580248/posts/259182268797023/

Lists are another type of object in Python. They are used to store an indexed list of items. A list is created using squ...
09/05/2020

Lists are another type of object in Python. They are used to store an indexed list of items.
A list is created using square brackets with commas separating items.
The certain item in the list can be accessed by using its index in square brackets.
For example:

-----------Program-------------
words = ["Hello", "world", "!"]
print(words[0])
print(words[1])
print(words[2])

-----------Output-------------
Hello
world
!

The first list item's index is 0, rather than 1, as might be expected.

An empty list is created with an empty pair of square brackets.

-----------Program-------------
empty_list = []
print(empty_list)

----------OUTPUT----------
[]

Most of the time, a comma won't follow the last item in a list. However, it is perfectly valid to place one there, and it is encouraged in some cases.

Typically, a list will contain items of a single item type, but it is also possible to include several different types.
Lists can also be nested within other lists.

-----------Program-------------
number = 3
things = ["string", 0, [1, 2, number], 4.56]
print(things[1])
print(things[2])
print(things[2][2])

----------OUTPUT----------
0
[1, 2, 3]
3

Lists of lists are often used to represent 2D grids, as Python lacks the multidimensional arrays that would be used for this in other languages.

Indexing out of the bounds of possible list values causes an IndexError.
Some types, such as strings, can be indexed like lists. Indexing strings behaves as though you are indexing a list containing each character in the string.
For other types, such as integers, indexing them isn't possible, and it causes a TypeError.

----------Program--------
str = "Hello world!"
print(str[6])

----------OUTPUT----------
w

For more about lists click here https://www.facebook.com/114683366580248/posts/259183262130257/

Writing programs that actually do what they are supposed to do is just one component of being a good Python programmer.I...
08/05/2020

Writing programs that actually do what they are supposed to do is just one component of being a good Python programmer.
It's also important to write clean code that is easily understood, even weeks after you've written it.

One way of doing this is to follow the Zen of Python, a somewhat tongue-in-cheek set of principles that serves as a guide to programming the Pythoneer way.

The Zen of Python, by Tim Peters

✓Beautiful is better than ugly.
✓Explicit is better than implicit.
✓Simple is better than complex.
✓Complex is better than complicated.
✓Flat is better than nested.
✓Sparse is better than dense.
✓Readability counts.
✓Special cases aren't special enough to break the rules.
✓Although practicality beats purity.
✓Errors should never pass silently , unless explicitly silenced.
✓In the face of ambiguity, refuse the temptation to guess.
✓There should be one-- and preferably only one --obvious way to do it.
✓Although that way may not be obvious at first unless you're Dutch.
✓Now is better than never.
✓Although never is often better than *right* now.
✓If the implementation is hard to explain, it's a bad idea.
✓If the implementation is easy to explain, it may be a good idea.
✓Namespaces are one honking great idea -- let's do more of those!

Some lines in the Zen of Python may need more explanation.
Explicit is better than implicit: It is best to spell out exactly what your code is doing. This is why adding a numeric string to an integer requires explicit conversion, rather than having it happen behind the scenes, as it does in other languages.
Flat is better than nested: Heavily nested structures (lists of lists, of lists, and on and on…) should be avoided.
Errors should never pass silently: In general, when an error occurs, you should output some sort of error message, rather than ignoring it.

There are 20 principles in the Zen of Python, but only 19 lines of text.
The 20th principle is a matter of opinion, but our interpretation is that the blank line means "Use whitespace".

The line "There should be one - and preferably only one - obvious way to do it" references and contradicts the Perl language philosophy that there should be more than one way to do it.

For more stuff like this follown here
https://www.facebook.com/pycoders88
PyCoders

Address


Alerts

Be the first to know and let us send you an email when PyCoders posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Contact The Business

Send a message to PyCoders:

  • Want your business to be the top-listed Media Company?

Share