payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Writing First Code   in Category: Python   by amit

🕙 Posted on 2023-06-21 at 05:04:45     Read in Hindi ...


How to run a program in Python?

    In the previous page, you have learned how to start the Python software (application), write some code in your project folder, and run it in command line interface (CLI) or VSCode Editor. You can also use Python IDLE (from your Start Menu in MS-Windows, for example), which also shows >>> (CLI). You can save your work in a file with .py extension. However, to write clean code, you should use better CODE Editor (VSCode) or IDE (PyCharm). An illustration of Python IDLE is shown below:

Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec 6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> print('Hello World')
Hello World

Variables

    Variables are names of memory (RAM) locations, which refer to respective memory locations of values stored in them. Variables are very important to run any program. Unlike PHP, a variable name in Python doesn't start with $ symbol. However, other rules for nomenclature of a variable name is similar to those of PHP and JavaScript.

    A variable name must not start with a number. It must not contain any special character, including blank space, and - (minus) symbol. A variable name must start with a English alpha­betical character or underscore, and may contain numbers within the variable name. Variable names are also case-sensitive.

Data Types

    Value assigned to a variable name may be of different data-types, for example, string (series of characters within a pair of single quotes or a pair of double quotes), integer (whole numbers, without decimal point), float (or double which may contain fractional value after the decimal point/dot), boolean (only True and False are boolean values - unlike PHP and JavaScript, in Python these two are case-sensitive). These four data-types are called as scalar data-types. Other data-types, such as compound data-types are different from PHP and JavaScript.

Indentation & Syntax

    Python programming language is developed to write codes in simplest form. It is designed to be used by scientists, professors and students, who know very less about compile-time programming languages. Python is execution-ready RUN-TIME programming language. Unlike other programming languages based on C, such as C++, Java, PHP, JavaScript etc. Python doesn't require a statement terminator.

    Therefore, each statement in Python programming language is written in a new line. A block of codes is generally enclosed in curly braces in other programming languages, but in Python you don't have to do so. Therefore, to identify a block of code, indentation is very important, and a specific pattern of blank spaces are used.

    Generally, codes in Python are written without any blank space before a statement (or expression). However, to write a block of codes, you have to leave a set of blank spaces after : (colon) symbol from next line. That is, when your CODE EDITOR by default is designed to leave four blank spaces, you have to leave blank spaces in sequence of 4, 8, 12, 16 for every NESTED block of codes.

    But, if you make your CODE EDITOR to leave two blank spaces (frequently in practice by many programmers), you have to leave blank spaces in sequence of 2, 4, 6, 8 for every NESTED block of codes. This makes your codes more intact and your Python file, for example, new.py will look nicer and compact.

Built-in Functions and Methods

    In the previous page, you have written a simple Python code, print( "Hello World" ) in >>> (CLI) as well as in VSCode Editor (and saved in new.py file). print() is a built-in function, used to display OUTPUT in the console. Every programming language has some reserved keywords and built-in functions. These built-in functions according to their behaviour, are classified into functions, methods, properties, constructs, etc.

Let's write some more Python codes

    Let's assign the string value used herein­above, that is, "Hello World" to a variable name, Greeting. You should either use a pair of single quotes or a pair of double quotes. But, you should not mix them, because the first single/double quote in each pair starts the value identified as string. Similarly, the second single/double quote in respective pair ends the series of characters.

    As of now, all Python codes are written in VSCode Editor, and saved in new.py file in python2023 sub-folder of the Project Folder, unless directed to save them in specific files. You can save your code in any folder as per your choice. However, you should use the Command Line Console, or VSCode Editor Terminal to run/execute these files, as explained in the previous page.

# Comments in Python are written with Hash symbol.
# Comments are written to understand what are the purpose of codes in your program.

Greeting = 'Hello World' # variable name Greeting has been assigned with a string data-type
print(Greeting) # variable used inside print() function

    You can use one or more variable names, string or other data-type literals (as explained in PHP context) inside the parentheses just after print, separating each with , (comma) symbol as shown below. You can see in above and below codes that NO indentation has been used till now.

name = 'Amit'
print('Hello', name, 'Welcome to Python')

    You can run both of above codes in CONSOLE or TERMINAL as shown below:

C:\Users\yourName>cd\
C:\>cd xampp\htdocs\python2023
C:\xampp\htdocs\python2023>python new.py

    The OUTPUT of above code is as follows:

Hello Amit Welcome to Python


Leave a Comment: