❌

Reading view

There are new articles available, click to refresh the page.

Learn Python - Day 2

Print Methods

Print() function is used to print the output in console and also it has the ability to handle formatting, special characters, Unicode and so on. We will see it one by one with examples

1. Basic usage

It prints the given text or data to the console.

Example

print("Hello, World!")

Output
Hello, World!

2. Multiple Objects

It prints multiple objects with space as separator by default.

Example

print("Hello", "World")

Output

Hello World

3. Sep Parameter
We can override the default separator space by using the sep parameter.

Example

print("Hello", "World", sep=", ")

Here we used comma space as separator between the objects. Likewise we can use anything as separator.

Output
Hello, World

4. End Parameter
We can set what is printed at the end of the output using end parameter. It set as newline by default (\n).
Example

print("Hello, World", end="!")
print(" How are you?")

Here we changed the new line parameter into exclamatory symbol using end parameter, so that both print methods are printed as single line as show below.

Output
Hello, World! How are you?

5. Printing variable
It prints the value of the variables in console
Example

 name = "Dinesh"
 age = 25
 print("Name:", name, "Age:", age)

Output
Name: Dinesh Age: 25

6. Formatted String Literals (f-strings)

We can include the expressions inside string literals using curly braces {}.
Example

age = 25
name = "Dinesh"
print(f"Name: {name}, Age: {age}")   

Output
Dinesh, Age: 25

7. String Formatting with % Operator
Using % operator we can format the strings.
Example

name = "Dinesh"
age = 25
print("Name: %s, Age: %d" % (name, age))

Output
Name: Dinesh, Age: 25

8. String Formatting with str.format()
Uses the str.format() method to format strings.

Example

name = "Dinesh"
age = 25
print("Name: {}, Age: {}".format(name, age))

Name: Dinesh, Age: 25

9. Printing with File
Redirects the output to a file.
Example

with open("d:\\output.txt", "w") as file:
    print("Hello, World!", file=file)

The text "Hello, world!" will write in txt file in the path "d:\output.txt"

10. Printing with Flush
Ensures the output is immediately flushed to the console.
Example

import time
for i in range(5):
 print(i, end=' ', flush=True)
time.sleep(1)

Output

0 1 2 3 4

11. Printing Special Characters
Prints special characters like tab (\t), newline (\n), and backslash (\).

Example

print("Tab\tSpace")
print("New\nLine")
print("Backslash\\Example")

Output

Tab Space
New
Line
Backslash\Example

Learn Python - Day 1

Chapter 1

What python can do?

  • Python is versatile programming language and known for its simplicity and readability
  • Python can do many things like web development, Data analysis and visualization, AI and ML, scripting for automation, Desktop GUI application, database access and so on.

Why python?

Python language simplicity, versatility, community support, and industry adoption make it a preferred choice for a wide range of applications, and it is now effectively used in the field of Finance, Data visualization, ML and AI.

Key points

  • Python syntax is easy to understand, and it resembles human language.
  • It supports multiple programming concepts like procedural, object-oriented and functional programming.
  • It has large active community of developers to support, knowledge sharing, contribute to python open-source projects and so on.
  • Python can run in various platforms like windows, Linux, IOS.
  • Python is an open-source software (Free to use)

Python syntax compared to other programming language.

  • Python syntax is concise and emphasizes readability with minimal syntactic terms and indentation is used to define the structure of the code, making it clear and organized.
  • In other programming languages, the explicit syntax with curly braces to define blocks of code and it requires more syntactic terms are used to code for method definitions class structures

Python Installation

windows:

  • Download the latest package from the below link or browse the supported package based on your windows OS.
  • https://www.python.org/ftp/python/3.12.4/python-3.12.4-amd64.exe
  • Double click exe file and proceed with installation in as per the instructions provided in the installation window and until successful installation

To verify installation

Open command prompt and type python, if it installed properly you will see the below information with version details.

C:\Users\Win11>python

Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.


Ubuntu:

  • Open the terminal window
  • Run the command "apt-get install python"
  • Follow the prompts to complete the installation
  • Alternatively, download the Python package for Ubuntu from the official Python website and install it manually.

*To verify installation *

  • open terminal
  • Type "python -version"
  • It shows the python version details

First python program with print() statement

  • open command prompt in windows and type python
  • Then type python command/code - Refer below image

Image description

Note:

❌