Monday, 22 January 2024

Python Learning Session-02 - Variables, Expressions, and Statements

 

Welcome to the second session of our Python course. In the previous session, we learned about the history and the inventor of the Python language. Today, we will learn more about some basic concepts in Python, such as constants, variables, expressions and statements. We will also see how to write and run code in an IDE (Integrated Development Environment).

Constants are values that do not change in a program. For example, the number 3.14 is a constant that represents the value of pi. In Python, constants are usually written in uppercase letters, such as PI = 3.14. However, this is not enforced by the language, and you can still change the value of a constant if you want to. It is just a good practice to use uppercase letters for constants to make your code more readable and avoid confusion.

Variables are names that refer to values that can change in a program. For example, you can create a variable called x and assign it a value of 10. Then, you can change the value of x to 20 later in the program. In Python, you do not need to declare the type of a variable before using it. You can simply use the assignment operator (=) to assign a value to a variable. For example, x = 10.

Expressions are combinations of values, variables and operators that produce a new value when evaluated. For example, x + 5 is an expression that adds the value of x and 5 and returns the result. Operators are symbols that perform certain operations on values or variables, such as + for addition, - for subtraction, * for multiplication, / for division, etc. 

Statements are instructions that tell the computer what to do. For example, print(x) is a statement that prints the value of x to the screen. Statements usually end with a newline character or a semicolon (;) in Python. 

To write and run code in Python, you need an IDE that provides tools for editing, debugging and executing your code. There are many IDEs available for Python, such as PyCharm, Visual Studio Code, Spyder, etc. You can choose any IDE that suits your preference and needs. To run your code in an IDE, you usually need to create a file with a .py extension and save your code in it. Then, you can use the run or execute command in the IDE to run your code and see the output.


Reserved Words

You cannot use reserved words as variable names / identifiers


One of the basic concepts in programming is the variable. A variable is a way of giving a name to a location in the memory where we can store some data. For example, we can create a variable called age and assign it the value 25. This means that in the memory, there is a place labeled age that holds the number 25. We can use the name age later in our program to access or modify the data stored there. We can also change the value of a variable by using a different assignment statement. For example, we can write age = 30 to update the value of age to 30. The name of the variable is chosen by the programmer, and it should be meaningful and descriptive of the data it represents.



Python Variable Name Rules

u  Must start with a letter or underscore _

u  Must consist of letters, numbers, and underscores

u  Case Sensitive

 


 

Expressions


        When we string operators together - Python must know which one to do first

        This is called “operator precedence”

        Which operator “takes precedence” over the others?

        x = 1 + 2 * 3 - 4 / 5 ** 6

 

Operator Precedence Rules

Highest precedence rule to lowest precedence rule:

u      Parentheses are always respected

u      Exponentiation (raise to a power)

u      Multiplication, Division, and Remainder

u      Addition and Subtraction

Left to right



        Remember the rules top to bottom

        When writing code - use parentheses

        When writing code - keep mathematical expressions simple enough that they are easy to understand

        Break long series of mathematical operations up to make them more clear

 

What Does “Type” Mean?

         In Python variables, literals, and constants have a “type”

         Python knows the difference between an integer number and a string

         For example “+” means “addition” if something is a number and “concatenate” if something is a string

concatenate = put together

 


Integer Division

Integer division produces a floating point result

Integer division produces a floating point result



Comments in Python

         Anything after a # is ignored by Python

         Why comment?

 -  Describe what is going to happen in a sequence of code

 -  Document who wrote the code or other ancillary information

 -  Turn off a line of code - perhaps temporarily



Thursday, 18 January 2024

Python Learning Session-01- Introduction

 

 
Title Python Learning Session-01- Introduction


Why Program?

when we will access any application, we thought

Why Program? what was the key aspect of it.

Now a days all things are going to online. day by day in the faster growing world we need to also update and aware about the thing are going around us, when we start to learning about the programming and development of any software are desktop application or is a web application.

 

Users vs. Programmers

 

         - Computers offer various tools for different purposes, such as writing, calculating, navigating, organizing, etc.

         - Programmers master the logic and the syntax of the computer language to create and modify these tools

         - Programmers use some tools to develop new tools that can be useful for themselves or for other users

         - Programmers can automate tasks by writing small programs that perform specific actions

Why be a Programmer?

- To get some task done - we are the user and programmer

  - Clean up survey data

    - Read the data from a CSV file

    - Remove any rows with missing or invalid values

    - Convert the data to a suitable format for analysis

    - Save the cleaned data to a new CSV file

- To produce something for others to use - a programming job

  - Fix a performance problem in the software

    - Identify the bottleneck in the code using a profiler

    - Optimize the algorithm or data structure that causes the slowdown

    - Test the code for correctness and efficiency

    - Document the changes and update the version control

    - Add a guestbook to a web site

...

 

    - Create a database table to store the guestbook entries

    - Write a python script to handle the form submission and validation

    - Write a python script to display the guestbook entries on the web page

    - Add some CSS and JavaScript to style and enhance the user interface

 

- We create software solutions for end users

- End users are our clients and benefactors - we aim to satisfy their needs and expectations - they pay us with money when they are happy

- We handle the data, information, and networks on behalf of the end users

- We use hardware and software as our tools and partners in this endeavor

 

Python IDE Interface



 

Hardware Architecture of the Software are working together with desire hardware


Process Flow diagram



         Central Processing Unit:  Runs the Program - The CPU is
always wondering “what to do next”.  Not the brains
exactly - very dumb but very very fast

         Input Devices:  Keyboard, Mouse, Touch Screen

         Output Devices:  Screen, Speakers, Printer, DVD Burner

         Main Memory:  Fast small temporary storage - lost on reboot - aka RAM

         Secondary Memory:  Slower large permanent storage - lasts until deleted - disk drive / memory stick

 

Process Flow Diagram about how to convert any program we writing any language are converting to the machine language binary code 0 and 1 format are shown here,  



Python as a Language

Parse tongue is the language of serpents and those who can converse with them.  An individual who can speak Parse tongue is known as a Parse mouth. It is a very uncommon skill, and may be hereditary. Nearly all known Parse mouths are descended from Salazar Slytherin.

Python is the language of the Python Interpreter and those who can converse with it. An individual who can speak Python is known as a Pythonista. It is a very uncommon skill, and may be hereditary. Nearly all known Pythonistas use software initially developed by Guido van Rossum.

 



 Early Learner: Syntax Errors

We need to learn the Python language so we can communicate our instructions to Python. You must remember that you are intelligent and can learn. The computer is simple and very fast, but cannot learn. So it is easier for you to learn Python than for the computer to learn English...


Basic Python IDE interface 





Sentences or Lines



Elements of Python:

# Vocabulary / Words - Variables and Reserved words

- Variables are names that refer to values stored in memory

- Reserved words are keywords that have a special meaning in the programming language and cannot be used as variable names

- Examples of reserved words in Python are: if, else, for, while, def, class, etc.


# Sentence structure - valid syntax patterns

- Syntax is the set of rules that define how to write statements and expressions in a programming language

- Valid syntax patterns are combinations of symbols, keywords, operators, and operands that form meaningful instructions for the computer

- Examples of valid syntax patterns in Python are: if condition: block, for variable in inerrable: block, def function_name(parameters): block, etc.


# Story structure - constructing a program for a purpose

- A program is a sequence of instructions that perform a specific task or solve a problem

- Constructing a program for a purpose involves defining the input, output, and logic of the program

- Examples of constructing a program for a purpose are: writing a function that calculates the area of a circle given its radius, writing a loop that prints the numbers from 1 to 10, writing a class that represents a person with attributes and methods, etc.

 

Reserved Words

You cannot use reserved words as variable names / identifiers

 


Python Scripts

 

              Interactive Python is good for experiments and programs of 3-4 lines long.

         Most programs are much longer, so we type them into a file and tell Python to run the commands in the file.

         In a sense, we are “giving Python a script”.

         As a convention, we add “.py” as the suffix on the end of these files to indicate they contain Python.

         Interactive

 -  You type directly to Python one line at a time and it responds

         Script

 -  You enter a sequence of statements (lines) into a file using a text  editor and tell Python to execute the statements in the file

         Like a recipe or installation instructions, a program is a sequence of steps to be done in order.

         Some steps are conditional - they may be skipped.

         Sometimes a step or group of steps is to be repeated.

         Sometimes we store a set of steps to be used over and over as needed several places throughout the program