0% found this document useful (0 votes)
12 views15 pages

Programming Fundamentals Lecture 02

Uploaded by

m86yc5bwvg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views15 pages

Programming Fundamentals Lecture 02

Uploaded by

m86yc5bwvg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Programming

Fundamentals
MPW 224
Lecture 02
Dr. Mohamed Elsabahy
Ph.D. in Energy Resource Engineering
Email: Mohamed_Elsabahy@eng.suez.edu.eg
Working with Variables in Python to
Manage Data
Getting Started With Python Lecture Content

☺Access Help, Autocompletion, etc.


☺Printing to the Console
☺Receive inputs from the Console
☺Variables
☺Comments
Getting Started With Python
Working with Variables in Python to Manage Data
Help, Autocompletion, etc.
Getting Started With Python
Working with Variables in Python to Manage Data
Printing to the Console in Python

`print()` Function
`print()` Function
- The `print()` function is used to output text (or other data) to the console or standard output device.
- It takes zero or more parameters as input and displays them on the console, separated by spaces by
default.
- It is commonly used for debugging, displaying information to users, or any other form of output.
- Syntax: `print(value1, value2, ..., sep=' ', end='\n', file=sys.stdout, flush=False)`
- `value1`, `value2`, ...: Any number of values or expressions to be printed.
- `sep`: Specifies the separator between the values. By default, it is a space `' '`.
- `end`: Specifies what to print at the end. By default, it is a newline `'\n'`.
- `file`: Specifies the output file. By default, it is the console (`sys.stdout`).
- `flush`: If `True`, the output buffer is flushed after printing. Default is `False`.
Getting Started With Python
Working with Variables in Python to Manage Data
Example
Getting Started With Python
Working with Variables in Python to Manage Data

Receive inputs from the Console


`input()` Function
`input()` Function
- The `input()` function is used to accept user input from the console or standard input device.
- It displays a prompt to the user and waits for them to enter text followed by pressing the Enter key.
- It always returns a string, which can be converted to other data types (e.g., int, float) as needed using
type conversion.
- Syntax: `input(prompt=None)`
- `prompt`: An optional string that is displayed as a prompt to the user. If not provided, the prompt is
omitted.
Getting Started With Python
Working with Variables in Python to Manage Data
Example
Getting Started With Python
Working with Variables in Python to Manage Data
Python Variables
Variable Naming
• Variable names can contain letters, digits, and underscores.
• They cannot start with a digit.
• Variable names are case-sensitive (myVariable, MyVariable, and myvariable are different variables).
• Variable names should be descriptive and meaningful to make the code more readable.

Assigning Values to Variables


• In Python, you can assign a value to a variable using the assignment operator =
x = 10
name = "Alice"
• The variable x now holds the value 10, and the variable name holds the string "Alice"
Getting Started With Python
Working with Variables in Python to Manage Data
Python Variables
Variable Types
• Python is dynamically typed, meaning you do not need to declare the type of a variable when you
create one. Python will automatically determine the data type based on the value assigned to it.
x = 10 # integer
y = 3.14 # float
name = "Bob" # string

• You can check the type of a variable using the type() function.
print(type(x)) # <class 'int’>
print(type(y)) # <class 'float’>
print(type(name)) # <class 'str'>

Variable Reassignment
• You can change the value of a variable by assigning it a new value.
x=5
x=x+1
• After executing these statements, the value of x will be 6.
Getting Started With Python
Working with Variables in Python to Manage Data
Python Variables
Multiple Assignment
• Python allows you to assign values to multiple variables in a single line.
a, b, c = 10, 20, 30
• This assigns 10 to a, 20 to b, and 30 to c.

Constants
• Although Python doesn't have built-in constant types, programmers often use variables with
uppercase names to denote constants.
PI = 3.14159
• By convention, constants are usually written in uppercase to distinguish them from regular
variables.
Getting Started With Python
Working with Variables in Python to Manage Data
Python Comments
What is Comment
• Comments are used to annotate code and provide explanations or documentation.
• Comments are ignored by the Python interpreter and are not executed as part of the program.
• They are purely for the benefit of developers reading and maintaining the code

Single-line Comments
• Single-line comments begin with the # symbol and continue until the end of the line.
• They are typically used for short explanations or annotations on specific lines of code.

Multi-line Comments
• Python does not have a specific syntax for multi-line comments like some other programming
languages.
• However, you can use multiple single-line comments or a string literal (triple quotes) to achieve a
similar effect.
• Multi-line comments are useful for longer explanations or documentation.
Getting Started With Python
Working with Variables in Python to Manage Data
Python Comments
Commenting Out Code
• Comments are also commonly used to temporarily disable or "comment out" lines of code during
development or debugging.
• This allows you to exclude specific lines from execution without deleting them.
Getting Started With Python
Working with Variables in Python to Manage Data
Python Comments
Example
Questions

You might also like