Week 2 Lab - Introduction to Python Programming PDF

Document Details

Uploaded by Deleted User

NU - Dasmariñas

Joevan U. Sacdalan

Tags

python programming introduction variables computer science

Summary

This document is a laboratory session on Python programming. It covers fundamental concepts such as variables, data types (integers, floats, strings), casting, and output methods. The laboratory session also includes introductory exercises for students.

Full Transcript

NU - DASMARIÑAS Introduction to Python Programming Introduction to Computing Week 8 1st Term 2024 - 2025 Joevan U. Sacdalan School of Engineering, Computing, and Architecture What is Python? a popular programming language created by Guido van Rossum, and released in 1991. It is used...

NU - DASMARIÑAS Introduction to Python Programming Introduction to Computing Week 8 1st Term 2024 - 2025 Joevan U. Sacdalan School of Engineering, Computing, and Architecture What is Python? a popular programming language created by Guido van Rossum, and released in 1991. It is used for web development (server-side), software development, mathematics, and system scripting. Why Python? Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.). Python has a simple syntax similar to the English language. Python has syntax that allows developers to write programs with fewer lines than some other programming languages. Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick. Python can be treated in a procedural way, an object-oriented way or a functional way. Python syntax compared to other programming languages Python was designed for readability, and has some similarities to the English language with influence from mathematics. Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose. Sample Python program Comments can be used to explain Python code, to make the code more readable, and to prevent execution when testing code. starts with a #, and Python will ignore them: Variables Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. Variables do not need to be declared with any particular type and can even change type after they have been set. Type Casting If you want to specify the data type of a variable, this can be done with casting. Single or Double Quotes? String variables can be declared either by using single or double quotes. Variable Names A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables) A variable name cannot be any of the Python keywords. Python keywords Multi-words Variable Names Variable names with more than one word can be difficult to read. There are several techniques you can use to make them more readable: 1. Camel Case – each word, except the first, starts with a capital letter 2. Pascal Case – each word starts with a capital letter 3. Snake Case – each word is separated by an underscore character: Output Variables The print() function is often used to output variables. You can output multiple variables, separated by a comma. You can also use the + operator to output multiple variables Output Variables For numbers, the + character works as a mathematical operator The best way to output multiple variables in the print() function is to separate them with commas, which even support different data types. Variable Exercises https://www.w3schools.com/python/exercise.asp?filename=exercise_v ariables1 Numbers Int Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. To verify the type of any object in Python, use the type() function. Numbers Float Float, or "floating point number" is a number, positive or negative, containing one or more decimals. Strings Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello". You can display a string literal with the print() function. Casting / Type Conversion int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number) Casting / Type Conversion float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer) Casting / Type Conversion str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals

Use Quizgecko on...
Browser
Browser